dotnet - Enumerating a Registry Key Results in a Null Value
Published: Dotnet Estimated reading time: ~1 minutes
This post assumes .NET 4.0
After trying for some time to get a registry key to list all of its subkeys, I kept getting a null
value back.
This code does not work
It always returns null
.
var keyName = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileGuid";
using (var guidSet = Registry.LocalMachine.OpenSubKey(keyName))
{
if (guidSet != null)
{
foreach (var guid in guidSet.GetSubKeyNames())
{
result.Add(guid);
Console.WriteLine("{0}", guid);
}
}
}
This code works
var keyName = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileGuid";
using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
using (var guidSet =hklm.OpenSubKey(keyName))
{
if (guidSet != null)
{
foreach (var guid in guidSet.GetSubKeyNames())
{
result.Add(guid);
Console.WriteLine("{0}", guid);
}
}
}
The difference is that the working code targets the x64 portion of the registry while the non-working code targets the default portion of the registry.
In this particular case, the ProfileGuid
subkey does not exist is the WOW6432Node
subkey of the registry.