0
public static bool IsApplictionInstalled(string p_name)
    {
        string displayName;
        RegistryKey key;

        // search in: CurrentUser
        key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
        foreach (String keyName in key.GetSubKeyNames())
        {
            RegistryKey subkey = key.OpenSubKey(keyName);
            displayName = subkey.GetValue("DisplayName") as string;
            if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
            {
                return true;
            }
        }

        // search in: LocalMachine_32
        key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
        foreach (String keyName in key.GetSubKeyNames())
        {
            RegistryKey subkey = key.OpenSubKey(keyName);
            displayName = subkey.GetValue("DisplayName") as string;
            if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
            {
                return true;
            }
        }

        // search in: LocalMachine_64
        key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
        foreach (String keyName in key.GetSubKeyNames())
        {
            RegistryKey subkey = key.OpenSubKey(keyName);
            displayName = subkey.GetValue("DisplayName") as string;
            if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
            {
                return true;
            }
        }

        // NOT FOUND
        return false;
    }

this method checks a software in 32 bit or 64 bit Win OS, but its not working, crushing at String keyName in key.GetSubKeyNames(), Object reference not set to an instance of an object. any one tell my what's the reason,

Desire
  • 563
  • 4
  • 13
  • 28
  • Perhaps the key does not exist? – ChrisBint Dec 01 '11 at 14:20
  • Aside from that, if things have gobe horribly wrong, the existance of the uninstall key, doesn't mean it's installed... – Tony Hopkinson Dec 01 '11 at 14:24
  • 2
    Please don't ever hardcode `Wow6432Node` into your application. If you are targetting .net 4 you can use the `RegistryView` enumeration to open either 32 or 64 bit views of the registry. – David Heffernan Dec 01 '11 at 14:25
  • Does this answer your question? [Check if application is installed in registry](https://stackoverflow.com/questions/16379143/check-if-application-is-installed-in-registry) – Cris Luengo Nov 20 '20 at 16:09

2 Answers2

4

I think the problem could be here:

key = Registry.LocalMachine.OpenSubKey(
          @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
foreach (String keyName in key.GetSubKeyNames())

On 32bit that key does not exist.
So you should use (for every key you check)

key = ....
if (key != null) 
{
    foreach (String keyName in key.GetSubKeyNames())
    // ....
}

Another info: notice that some (many?) registry keys don't contain displayName value, so your comparison could fail. Try (just as example) to use key name in place of displayName if this does not exist.

Marco
  • 56,740
  • 14
  • 129
  • 152
1

The error means that OpenSubKey returned a null (you are getting a NullReferenceException when you try to access a member of a variable set to null). This in turn means that the registry key you are looking for does not exist.

Add a null check before trying to use the key object.

key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
if(key != null)
{
    foreach (String keyName in key.GetSubKeyNames())
    {
      // .... 
    }
}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • i tried it, i run my code, i have installed skype, but this method always returns false – Desire Dec 01 '11 at 14:24
  • @Aqib: for what I know, not every software you install places a key on that part of the registry... – Marco Dec 01 '11 at 14:25
  • You asked about the error, though your title is different from your question. When reading from the registry, you need to make sure that the account that the program runs under has permissions to read from the registry. – Oded Dec 01 '11 at 14:26