93

I'm trying to get all the display names of the sub keys within this key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

With this 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);
     }

After running the code I can't find one of the keys I need:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}

And it should have the display name: Microsoft Visual C++ 2010 x64 Redistributable - 10.0.30319, but instead the GetSubKeyNames() method gives me the sub key : {DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}.KB2151757 which doesn't have any display name.

Why can't I get the exact sub key I need ({DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}) and how can I get it?

Zak Soliman
  • 1,130
  • 1
  • 7
  • 14

2 Answers2

215

A 32-bit application on a 64-bit OS will be looking at the HKLM\Software\Wow6432Node node by default. To read the 64-bit version of the key, you'll need to specify the 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
}

The API to do this was added in .NET 4.0; if you're still using 3.5, you'll need to use P/Invoke to access the 64-bit keys: http://www.rhyous.com/2011/01/24/how-read-the-64-bit-registry-from-a-32-bit-application-or-vice-versa/

Richard Deeming
  • 29,830
  • 10
  • 79
  • 151
  • 1
    Thanks. I had the same problem, with same key. :) – Eduardo Mar 18 '14 at 11:51
  • 2
    Thanks for this. Although I still don't understand why there are two registries. – Robert Noack Jul 08 '15 at 19:37
  • @RobertNoack: I think it's mainly for COM, and to ensure that environment variables get expanded correctly in `REG_EXPAND_SZ` values. There's some information on MSDN - [Registry Redirector](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384232(v=vs.85).aspx) – Richard Deeming Jul 08 '15 at 20:25
  • 1
    You saved my day. I was getting freaked out with the way registry key was behaving for me. +1. – RBT Jan 08 '17 at 13:24
  • I must have read a dozen explainers; this is the first to show the RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) step. Thanks!! – Joe Baker Feb 18 '20 at 14:01
  • I run with Debug\x64 dotnet 4.6.1, but problem still exists. I can read the same path in another C# project with the same code. – Acewind Oct 07 '20 at 13:49
23

In Visual Studio 2017 go to

Project > Properties > Build > Uncheck **Prefer 32-bit** and Platform target as **Any CPU**.
Amen Jlili
  • 1,884
  • 4
  • 28
  • 51
Clint
  • 6,011
  • 1
  • 21
  • 28