Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

registry

get

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");
                }
like image 394
NightsEVil Avatar asked Jul 24 '10 22:07

NightsEVil


People also ask

Which is used to check the existence of key in registry?

RegRead(strKey) to detect key existence (as opposed to value existance) consider the following (as observed on Windows XP): If strKey name is not the name of an existing registry path, Err.

How do I find the registry key in PowerShell?

One of the easiest ways to find registry keys and values is using the Get-ChildItem cmdlet. This uses PowerShell to get a registry value and more by enumerating items in PowerShell drives. In this case, that PowerShell drive is the HKLM drive found by running Get-PSDrive .

What is the registry entry?

REG files. .REG files (also known as Registration entries) are text-based human-readable files for exporting and importing portions of the registry using an INI-based syntax. On Windows 2000 and later, they contain the string Windows Registry Editor Version 5.00 at the beginning and are Unicode-based.


2 Answers

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");
    }
like image 107
jwismar Avatar answered Oct 20 '22 01:10

jwismar


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");
}
like image 34
Sani Singh Huttunen Avatar answered Oct 19 '22 23:10

Sani Singh Huttunen