如何在 Vista 中读取注册表分支 HKEY_LOCAL_MACHINE? [英] How to read registry branch HKEY_LOCAL_MACHINE in Vista?
问题描述
我将应用程序设置存储在 HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany 分支下.不同用户的设置必须相同,这就是设置不在 HKEY_CURRENT_USER 下的原因.注册表值仅在应用程序使用期间读取.
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.
现在,在 Windows Vista 中,由于 UAC,您不能再使用以下代码读取注册表值:
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");
如何从我的代码 (C#) 中的 LocalMachine 分支读取值?
How can I read the values from LocalMachine branch in my code (C#)?
推荐答案
问题是您正在尝试创建一个未读取的密钥.如果您使用适当的 API,您应该能够在 Vista 上从 HKLM 读取值.
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);
注意上面的 false 参数.这具有以只读模式打开密钥的效果.这是 OpenSubKey 的默认设置,但我更喜欢显式设置(主要是因为我不记得默认设置).
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).
这篇关于如何在 Vista 中读取注册表分支 HKEY_LOCAL_MACHINE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!