Registry.GetValue 有什么问题?
我试图获取注册表值:
var value = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography", "MachineGuid", 0);
在 Windows XP 中一切正常,但在 Windows 7 中返回 0。在使用 regedit 的 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography
中,我看到 MachineGuid
,但是如果我运行
var keys = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("Cryptography", RegistryKeyPermissionCheck.ReadSubTree).GetValueNames();
keys.Length
为 0。
我做错了什么?其他值在两个操作系统中都可以。
I trying to get a registry value:
var value = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography", "MachineGuid", 0);
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
is 0.
What do I do wrong? With other values all ok in both of OS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
问题是您可能正在将解决方案编译为 x86,如果您编译为 x64,则可以读取这些值。
尝试将以下代码编译为 x86 和 x64:
您可以阅读有关访问备用注册表视图的更多信息。
您可以在 这里是一种读取 x86 和 x64 值的方法。
The problem is that you probably are compiling the solution as x86, if you compile as x64 you can read the values.
Try the following code compiling as x86 and x64:
You can read more about Accessing an Alternate Registry View.
You can found in here a way of reading values in x86 and x64.
这可能与UAC(用户帐户控制)有关。 Windows Vista 和 Windows 7 的额外保护层。
您需要向注册表请求权限。
编辑:
您现在的代码:
仅请求加密子项的权限,也许这会导致问题(至少我遇到过一次),因此新代码将是:
EDIT2:
我在这段代码上附加了调试器:
事实证明,您可以读取该特定值,至少这是我的猜测,因为所有数据都是正确的,直到我打开 key3,ValueCount 为零,而不是预期的 1我
认为这是受保护的特殊价值。
It probably has to do with UAC (User Account Control). The extra layer of protection for Windows Vista and Windows 7.
You'll need to request permissions to the registry.
EDIT:
Your code right now:
Only requests the permissions on the Cryptography subkey, maybe that causes the problem (at least I had that once), so the new code would then be:
EDIT2:
I attached the debugger to it, on this code:
It turns out, you can read that specific value, at least that's my guess, because all data is correct, until I open key3, there the ValueCount is zero, instead of the expected 1.
I think it's a special value that's protected.
您说您使用的是 64 位 Windows:您的应用程序是 32 位吗?如果是这样,它可能受到注册表重定向的影响,并且正在查看
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Cryptography
。您可能需要 P/Invoke 才能解决此问题:http://msdn.microsoft .com/en-us/library/aa384129.aspx。You say you're on 64-bit Windows: is your app 32-bit? If so it's probably being affected by registry redirection and is looking at
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Cryptography
. You may have to P/Invoke to work around it: http://msdn.microsoft.com/en-us/library/aa384129.aspx.如果您不是管理员,您只有 HKLM 的读取权限。您需要以只读方式打开密钥。不知道如何使用 .NET 的注册表类来做到这一点;直接使用 API,您可以使用 RegOpenKeyEx() 带有KEY_READ
标志。编辑:检查 MSDN 后,我看到 OpenSubKey() 是只读打开,成功则返回内容,失败则不返回任何内容。由于您要链接多个 OpenSubKey 调用,因此很可能其中一个调用失败,从而导致其他调用失败。尝试将它们分解为单独的调用,并检查返回的中间值。
If you're not an administrator, you only have read permission on HKLM. You need to open the key read-only instead. Not sure how to do that with .NET's Registry class; with the API directly, you use RegOpenKeyEx() with theKEY_READ
flag.EDIT: 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.
也许聚会有点晚了,但是,没有一个解决方案对我有用。
这就是我解决这个问题的方法:
Maybe a little late to the party, but, none of the solutions worked for me.
This is how I've solved this issue:
当我导入 Microsoft.Win32 并将应用程序设置更改为 x64(如 pedrocgsousa 提到的)时,我解决了问题。
I solved the problem when i imported Microsoft.Win32 and changed the application-settings to x64 like pedrocgsousa mentioned.