C# OpenSubKey() 为我可以在 regedit.exe 中看到的注册表项返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13728491/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:36:10  来源:igfitidea点击:

OpenSubKey() returns null for a registry key that I can see in regedit.exe

c#registry

提问by Zak Soliman

I'm trying to get all the display names of the sub keys within this key:

我正在尝试获取此键中子键的所有显示名称:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

With this code:

使用此代码:

     RegistryKey newKey;
     string val;

     string KeyPath64Bit = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
     RegistryKey mainKey = Registry.LocalMachine.OpenSubKey(KeyPath64Bit);

     string[] RegKeys64Bits = Registry.LocalMachine.OpenSubKey(KeyPath64Bit).GetSubKeyNames();

     foreach (string s in RegKeys64Bits)
     {
        newKey = mainKey.OpenSubKey(s);
        val = newKey.GetValue("DisplayName", -1, RegistryValueOptions.None).ToString();
        if (val != "-1")
           file64.WriteLine(val);
     }

After running the code I can't find one of the keys I need:

运行代码后,我找不到我需要的键之一:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}

And it should have the display name: Microsoft Visual C++ 2010 x64 Redistributable - 10.0.30319, but instead the GetSubKeyNames()method gives me the sub key : {DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}.KB2151757which doesn't have any display name.

它应该有显示名称:Microsoft Visual C++ 2010 x64 Redistributable - 10.0.30319,但该GetSubKeyNames()方法给了我子键:{DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}.KB2151757它没有任何显示名称。

Why can't I get the exact sub key I need ({DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}) and how can I get it?

为什么我无法获得所需的确切子密钥 ( {DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}) 以及如何获得它?

采纳答案by Richard Deeming

A 32-bit application on a 64-bit OS will be looking at the HKLM\Software\Wow6432Nodenode by default. To read the 64-bit version of the key, you'll need to specify the RegistryView:

HKLM\Software\Wow6432Node默认情况下,64 位操作系统上的 32 位应用程序将查看节点。要读取 64 位版本的密钥,您需要指定RegistryView

using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
using (var key = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"))
{
   // key now points to the 64-bit key
}

The API to do this was added in .NET 4.0; if you're still using 3.5, you'll need to use P/Invoke to access the 64-bit keys: http://www.rhyous.com/2011/01/24/how-read-the-64-bit-registry-from-a-32-bit-application-or-vice-versa/

.NET 4.0 中添加了执行此操作的 API;如果您仍在使用 3.5,则需要使用 P/Invoke 来访问 64 位密钥:http: //www.rhyous.com/2011/01/24/how-read-the-64-bit -registry-from-a-32-bit-application-or-vice-versa/

回答by Clint

In Visual Studio 2017 go to

在 Visual Studio 2017 中转到

Project > Properties > Build > Uncheck 32 bit and Platform target as "Any CPU".