NightsEVil
NightsEVil

Reputation: 517

Get if Registry entry exists if so do this, if not do that

So in my registry I have the entry under "LocalMachine\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\" called "COMODO Internet Security" which is my firewall. Now what i'd like to know is how can i get the registry to check if that entry exists? If it does do this if not then do that. I know how to check if the subkey "Run" exists but not the entry for "COMODO Internet Security", this is the code I was using to get if the subkey exists.

                using (RegistryKey Key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\"))
                if (Key != null)
                {

                    MessageBox.Show("found");
                }
                else
                {
                    MessageBox.Show("not found");
                }

Upvotes: 9

Views: 41087

Answers (5)

CursedSheep
CursedSheep

Reputation: 138

My code

        private void button2_Click(object sender, EventArgs e)

    {
        string HKCUval = textBox1.Text;
        RegistryKey HKCU = Registry.CurrentUser;
        //Checks if HKCUval exist.
        try {
            HKCU.DeleteSubKey(HKCUval); //if exist.
        }
        catch (Exception)
        {
            MessageBox.Show(HKCUval + " Does not exist"); //if does not exist.
        }

        }

Hope it helps.

Upvotes: 0

Sani Huttunen
Sani Huttunen

Reputation: 24385

Try this:

using (RegistryKey Key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\COMODO Internet Security"))
{
  if (Key != null)
    MessageBox.Show("found");
  else
    MessageBox.Show("not found");
}

Upvotes: 1

kayleeFrye_onDeck
kayleeFrye_onDeck

Reputation: 6958

I ran into an issue recently where I was trying to grab subkeys in a registry entry, but the problem was that since I was iterating over every registry key in that section of the registry, sometimes values would not have the subkey I was looking for, and I would get a null reference exception when trying to evaluate the subkey's value.

So, very similar to what some other answers provided, this is what I ended up going with:

string subkeyValue = null;

var subKeyCheck = subkey.GetValue("SubKeyName");

if(subKeyCheck != null)
{
    subkeyValue = subkey.GetValue("SubKeyName").ToString();
}

So depending on what subkey value you're looking for, just swap it out for "SubKeyName" and this should do the trick.

Upvotes: 0

jwismar
jwismar

Reputation: 12258

If you're looking for a value under a subkey, (is that what you mean by "entry"?) you can use RegistryKey.GetValue(string). This will return the value if it exists, and null if it doesn't.

For example:

using (RegistryKey Key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\"))
    if (Key != null)
    {    
        string val = Key.GetValue("COMODO Internet Security");
        if (val == null)
        {
            MessageBox.Show("value not found");
        }
        else
        {
            // use the value
        }
    }
    else
    {
        MessageBox.Show("key not found");
    }

Upvotes: 9

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102398

The following link should clarify this:

How to check if a registry key / subkey already exists

Sample code:

using Microsoft.Win32;

RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software\\Geekpedia\\Test");

if(rk != null)
{
   // It's there
}
else
{
   // It's not there
} 

Upvotes: 0

Related Questions