Unable to Open Registry Key in OpenSubKey()
When attempting to retrieve display names for subkeys within the registry key HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionUninstall, using the 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); }
One specific subkey, {DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}, remains elusive. Instead, GetSubKeyNames() returns {DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}.KB2151757, a subkey that lacks a display name.
Cause:
On a 64-bit operating system, a 32-bit application by default accesses the HKLMSoftwareWow6432Node registry key. To retrieve the 64-bit version of the key containing the desired subkey, you need to specify RegistryView.Registry64.
Solution:
In .NET 4.0 and above, you can open the 64-bit registry key using the RegistryView property:
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 }
For .NET 3.5, you can utilize P/Invoke to access 64-bit keys. Refer to the documentation at the following link for detailed instructions:
http://www.rhyous.com/2011/01/24/how-read-the-64-bit-registry-from-a-32-bit-application-or-vice-versa/
The above is the detailed content of Why Can't My 32-bit Application Access the 64-bit Registry Key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall?. For more information, please follow other related articles on the PHP Chinese website!