0

I'm trying to check if certain programs are installed using the registry and I hit a problem, Sometimes it works and sometimes it doesn't and I don't know why .. can someone tell me what am I doing wrong here ?

This is the code I'm using

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 just call it by

if (checkInstalled("XXXXX"))
        {
            //do stuff
        }

it doesn't always work .. for example if I try to check for "Riot Vanguard" it will not work and it will return false even if it's installed. and here is the regkey for it.

Ahmed Rizk
  • 11
  • 2

1 Answers1

0

I know that's a long shot and might not matter, but whenever I have problems in code that uses collections created with Linq I like to put .ToList() at the end to make sure that the whole query executes before proceeding (in your case I mean collections in the foreach loops)