Search code examples
c#registry64-bit.net-2.0

Getting list of installed programs on 64-bit Windows


I am using this code to make a list of all the installed programs:

object line;
string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
{
    foreach (string subkey_name in key.GetSubKeyNames())
    {
        using (RegistryKey subkey = key.OpenSubKey(subkey_name))
        {
            line = subkey.GetValue("DisplayName");
            if (line != null)
            {
                listBox1.Items.Add(line);
            }
        }
    }
}

On a 64-bit windows, this redirects to Wow6432Node \Microsoft\Windows\CurrentVersion\Uninstall. But some program entries are still located in the original path and the list is incomplete. How can I avoid redirection and read the values from both paths on a 64-bit Windows installation (and only the first one on a 32-bit windows)?


Solution

  • Change your code to the following:

            object line;
            string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            using (var baseKey = Microsoft.Win32.RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
            {
                using (var key = baseKey.OpenSubKey(registry_key))
                {
                    foreach (string subkey_name in key.GetSubKeyNames())
                    {
                        using (var subKey = key.OpenSubKey(subkey_name))
                        {
                            line = subKey.GetValue("DisplayName");
                            if (line != null)
                            {
                                listBox1.Items.Add(line);
                            }
                        }
                    }
                }
            }
    

    And you can either specify

    RegistryView.Registry64
    

    or

    RegistryView.Registry32
    

    explicitly, rather than letting it default to whatever it likes.