How to open a WOW64 registry key from a 64-bit .NET application

Solution 1:

If you can change the target .Net version to v4, then you can use the new OpenBaseKey function e.g.

RegistryKey registryKey;
if (Environment.Is64BitOperatingSystem == true)
{
    registryKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
}
else
{
    registryKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
}

Solution 2:

The correct way would be to call the native registry api and passing the KEY_WOW64_32KEY flag to RegOpenKeyEx/RegCreateKeyEx

Solution 3:

In the case where you explicitly need to read a value written by a 32 bit program in a 64 bit program, it's OK to hard code it. Simply because there really is no other option.

I would of course abstract it out to a helper function. For example

public RegistryKey GetSoftwareRoot() {
  var path = 8 == IntPtr.Size 
    ? @"Software\Wow6432Node"
    : @"Software";
  return Registry.CurrentUser.OpenSubKey(path);
}