When attempting to retrieve subkey display names within the registry key HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionUninstall, users may encounter an issue where the OpenSubKey() method returns null for a specific subkey visible in the Registry Editor (regedit.exe).
The underlying cause lies in the default registry view used by 32-bit applications on 64-bit operating systems. By default, these applications access the HKLMSoftwareWow6432Node node, which contains 32-bit registry keys. To read the 64-bit version of the key, it is necessary to specify the RegistryView when opening the base key.
To resolve this issue, utilize the following code:
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 }
This code ensures that the registry key is opened using the 64-bit view, allowing access to the desired subkey and its display name.
Alternatively, for .NET 3.5 users, it is necessary to resort to P/Invoke to access 64-bit keys, as described in the resource provided in the original answer.
The above is the detailed content of Why Does OpenSubKey() Return Null for a Registry Key Visible in Regedit?. For more information, please follow other related articles on the PHP Chinese website!