Search code examples
c#windowsregistry

Check if an application is installed in the registry


I use this to list all the applications listed in the registry for 32bit & 64, and display them in the console window:

string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey);
if (key != null)
{
    foreach (String a in key.GetSubKeyNames())
    {
        RegistryKey subkey = key.OpenSubKey(a);
        Console.WriteLine(subkey.GetValue("DisplayName"));
    }
}

registryKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
key = Registry.LocalMachine.OpenSubKey(registryKey);
if (key != null)
{
    foreach (String a in key.GetSubKeyNames())
    {
        RegistryKey subkey = key.OpenSubKey(a);
        Console.WriteLine(subkey.GetValue("DisplayName"));
    }
}

I am trying to find one program title out of the list of display names to see if it's installed.

The last thing I tried was:

if (subkey.Name.Contains("OpenSSL"))
    Console.Writeline("OpenSSL Found");
else
    Console.Writeline("OpenSSL Not Found");

Anything I tried, came back either false or a false positive.

How do I grab a title out of the list?

private static void IsApplicationInstalled(p_name) function does not work for.


Solution

  • After searching and troubleshooting, I got it to work this way:

    public static bool checkInstalled (string c_name)
    {
        string displayName;
    
        string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
        RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey);
        if (key != null)
        {
            foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
            {
                displayName = subkey.GetValue("DisplayName") as string;
                if (displayName != null && displayName.Contains(c_name))
                {
                    return true;
                }
            }
            key.Close();
        }
    
        registryKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
        key = Registry.LocalMachine.OpenSubKey(registryKey);
        if (key != null)
        {
            foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
            {
                displayName = subkey.GetValue("DisplayName") as string;
                if (displayName != null && displayName.Contains(c_name))
                {
                    return true;
                }
            }
            key.Close();
        }
        return false;
    }
    

    And I simply just call it using

    if(checkInstalled("Application Name"))