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

Zak*_*man 71 c# registry

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

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
Run Code Online (Sandbox Code Playgroud)

使用此代码:

     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);
     }
Run Code Online (Sandbox Code Playgroud)

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

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}
Run Code Online (Sandbox Code Playgroud)

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

为什么我不能得到我需要的确切子键({DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}),我怎么能得到它?

Ric*_*ing 166

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
}
Run Code Online (Sandbox Code Playgroud)

在.NET 4.0中添加了执行此操作的API; 如果您仍在使用3.5,则需要使用P/Invoke来访问64位密钥:http: //www.rhyous.com/2011/01/24/how-read-the-64 -bit -registry-从-A-32位应用程序或-反之亦然/

  • 谢谢你。虽然我还是不明白为什么有两个注册表。 (2认同)

Cli*_*int 8

在Visual Studio 2017中,转到项目>属性>生成>取消选中32位并将平台目标设置为``任何CPU''。

  • 我有同样的问题 - 我取消选中 32 位,它工作正常。 (3认同)