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

need help with code to check if a registry value exists in a subkey if

Page 1 of 1

10 Replies - 10949 Views - Last Post: 28 July 2010 - 06:50 PM Rate Topic: -----

#1 NightsEvil  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 85
  • Joined: 23-May 10

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

Posted 24 July 2010 - 10:45 PM

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");
                }


the closest iv gotten is this:

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");
    }


but i get this error code "Error 1 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)" any help?

Is This A Good Question/Topic? 0
  • +

Replies To: Get if Registry entry exists if so do this, if not do that

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

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

Posted 24 July 2010 - 10:49 PM

Change this line

string val = Key.GetValue("COMODO Internet Security");



To this

string val = (string)Key.GetValue("COMODO Internet Security");


Was This Post Helpful? 1
  • +
  • -

#3 NightsEvil  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 85
  • Joined: 23-May 10

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

Posted 24 July 2010 - 11:06 PM

ok well that fixed the error (thank you very much) but for some reason the whole code isnt working properly. ok so i have this:

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


and im getting "value not found" but when i go look in regedit under Local Machine\Software\Windows\CurrentVersion\Run and it is there.
Was This Post Helpful? 0
  • +
  • -

#4 sithius92  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 164
  • Joined: 01-August 08

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

Posted 27 July 2010 - 01:12 PM

Try changing this

if (val == null)


to this

if (val == string.Empty)

Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

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

Posted 27 July 2010 - 01:47 PM

View Postsithius92, on 27 July 2010 - 04:12 PM, said:

Try changing this

if (val == null)


to this

if (val == string.Empty)


or change it to..

if (string.IsNullOrEmpty(val))
{

}



now it checks both.

This post has been edited by eclipsed4utoo: 27 July 2010 - 06:44 PM

Was This Post Helpful? 0
  • +
  • -

#6 NightsEvil  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 85
  • Joined: 23-May 10

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

Posted 27 July 2010 - 05:53 PM

when i tried to use

if (string.IsNullorEmpty(val))
{

}




i got "Error 1 'string' does not contain a definition for 'IsNullorEmpty'"
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

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

Posted 27 July 2010 - 05:58 PM

Well it exists, string.IsNullOrEmpty. Show the exact code and how you're using it
Was This Post Helpful? 0
  • +
  • -

#8 NightsEvil  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 85
  • Joined: 23-May 10

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

Posted 27 July 2010 - 06:08 PM

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


also do i have the value found, and value not found in the correct spots?
Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

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

Posted 27 July 2010 - 06:45 PM

View PostNightsEvil, on 27 July 2010 - 08:53 PM, said:

when i tried to use

if (string.IsNullorEmpty(val))
{

}




i got "Error 1 'string' does not contain a definition for 'IsNullorEmpty'"


I had a typo. it's IsNullOrEmpty
Was This Post Helpful? 2
  • +
  • -

#10 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

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

Posted 27 July 2010 - 09:23 PM

Nice catch eclipsed4utoo I didn't even notice that. That should solve your error NightsEvil
Was This Post Helpful? 0
  • +
  • -

#11 NightsEvil  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 85
  • Joined: 23-May 10

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

Posted 28 July 2010 - 06:50 PM

im still getting "value not found" but when i go look in regedit under Local Machine\Software\Windows\CurrentVersion\Run,it is there. why?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1