How to read registry branch HKEY_LOCAL_MACHINE in Vista?

淺唱寂寞╮ 提交于 2019-12-10 05:43:41

问题


I have Application settings stored under HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany branch. Settings must be same for different users and that is the reason why settings are not under HKEY_CURRENT_USER. Registry values are only read during use of application.

Now, in Windows Vista and due to UAC you can't anymore use following code to read registry values:

RegistryKey myKey = Registry.LocalMachine.CreateSubKey
        ("SOFTWARE\\MyCompany\\MyAppName");

How can I read the values from LocalMachine branch in my code (C#)?


回答1:


The problem is that you are trying to create a key not read it. You should be able to read values from HKLM just fine on Vista if you use the appropriate API.

RegistryKey myKey = Registry.LocalMachine.OpenSubKey(
  @"Software\MyCompany\MyAppName", 
  false);

Notice the false parameter in the above. This has the effect of opening the key in a read only mode. This is the default setting for OpenSubKey but I prefer to be explicit (mainly because I can't ever remember the default).



来源:https://stackoverflow.com/questions/877851/how-to-read-registry-branch-hkey-local-machine-in-vista

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!