请告诉我错Registry.GetValue?告诉我、Registry、GetValue

2023-09-03 16:11:24 作者:射你一脸当面膜

我试图让一个注册表值:

I trying to get a registry value:

var value = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography", "MachineGuid", 0);

在Windows XP中都可以,但是在Windows 7返回0。 HKEY_LOCAL_MACHINE \ SOFTWARE \微软\加密使用注册表编辑器我看到的MachineGuid ,但如果我跑

In Windows XP all ok, but in Windows 7 returns 0. In HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography using regedit I see MachineGuid, but if I run

var keys = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("Cryptography", RegistryKeyPermissionCheck.ReadSubTree).GetValueNames();

keys.Length 0

我该怎么办错了吗?与其他值都可以在这两个操作系统。

What do I do wrong? With other values all ok in both of OS.

推荐答案

如果你不是一个管理员,你只有读取HKLM权限。你需要开关键只读代替。不知道怎么做,与.NET的注册类;直接的API,可以使用 RegOpenKeyEx的()与 KEY_READ 标记。

编辑:检查MSDN之后,我看到 OpenSubKey()不以只读方式打开,并返回的内容,如果它成功,仅此而已,如果它失败。既然你链接多个OpenSubKey调用,这是最有可能他们中的一个说是失败,导致其他失败。尝试打破他们分为不同的电话,并检查中间值返回。

After checking MSDN, I see that OpenSubKey() does open read only, and returns the contents if it succeeds and nothing if it fails. Since you're chaining multiple OpenSubKey calls, it's most likely one of them that's failing that causes the others to fail. Try breaking them out into separate calls, and checking the intermediate values returned.

 
精彩推荐