C# 如何在 Vista 中读取注册表分支 HKEY_LOCAL_MACHINE?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/877851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read registry branch HKEY_LOCAL_MACHINE in Vista?
提问by Clack
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 readduring use of application.
我将应用程序设置存储在 HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany 分支下。不同用户的设置必须相同,这就是设置不在 HKEY_CURRENT_USER 下的原因。注册表值仅在应用程序使用期间读取。
Now, in Windows Vista and due to UAC you can't anymore use following code to read registry values:
现在,在 Windows Vista 中,由于 UAC,您不能再使用以下代码来读取注册表值:
RegistryKey myKey = Registry.LocalMachine.CreateSubKey
("SOFTWARE\MyCompany\MyAppName");
How can I read the values from LocalMachine branch in my code (C#)?
如何从我的代码 (C#) 中的 LocalMachine 分支读取值?
采纳答案by JaredPar
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.
问题是您正在尝试创建一个未读取的密钥。如果您使用适当的 API,您应该能够在 Vista 上从 HKLM 读取值就好了。
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).
注意上面的 false 参数。这具有以只读模式打开密钥的效果。这是 OpenSubKey 的默认设置,但我更喜欢显式设置(主要是因为我不记得默认设置)。