tags:

views:

142

answers:

3

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 id like to know is how cani 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 useing 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");
                }
A: 

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");
    }
jwismar
ok and how do i get it to look in localmachine with getvalue?
NightsEVil
Adding example.
jwismar
Error 1 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
NightsEVil
OK, try casting the result to string. `string val = (string)Key.GetValue("COMODO Internet Security");`
jwismar
ok well that fixed the error (thank you very much) but for some reason the whole code is not working properly. Im getting "value not found" but when i go look in regedit under Local Machine\Software\Windows\CurrentVersion\Run and it is there.
NightsEVil
A: 

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
} 
Leniel Macaferi
but i need to look for a specific start up entry under local machine current user microsoft windows Run
NightsEVil
A: 

This will do it:

using (RegistryKey Key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsof\Windows\CurrentVersion\Run\COMODO Internet Security"))
{
  if (Key != null)
    MessageBox.Show("found");
  else
    MessageBox.Show("not found");
}
Sani Huttunen
but i need to look for a specific start up entry under local machine current user microsoft windows Run
NightsEVil