我正在尝试获取此键中子键的所有显示名称:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
使用这段代码:
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);
}
运行代码后我找不到我需要的键之一:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}
它应该有显示名称:Microsoft Visual C++ 2010 x64 Redistributable - 10.0.30319,但是 GetSubKeyNames()
方法给我子键:{DA5E371C-6333- 3D8A-93A4-6FD5B20BCC6E}.KB2151757
没有任何显示名称。
为什么我无法获得我需要的确切子 key ({DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}
),我该如何获得它?
最佳答案
默认情况下,64 位操作系统上的 32 位应用程序将查看 HKLM\Software\Wow6432Node
节点。要读取 64 位版本的 key ,您需要指定 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
}
.NET 4.0 中添加了执行此操作的 API;如果您仍在使用 3.5,则需要使用 P/Invoke 来访问 64 位 key : http://www.rhyous.com/2011/01/24/how-read-the-64-bit-registry-from-a-32-bit-application-or-vice-versa/
关于c# - OpenSubKey() 为我在 regedit.exe 中看到的注册表项返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13728491/