locked
Checking for registry value RRS feed

  • Question

  • How will I do a code that will check if a certain registry value is currently in the registry, if not create it.

    I did somethign like this but I get an error:


    if (Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run").GetValue("CA", "C:\\windows\\consoleapplication2.exe"); == false)
    {
    Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true).SetValue("CA", "C:\\windows\\consoleapplication2.exe");
    }

    Friday, October 6, 2006 1:47 AM

Answers

All replies

  • take a look here on how to get a registry value:

    http://msdn2.microsoft.com/en-us/library/microsoft.win32.registrykey.aspx

    the above link also shows how to delete a reg sub key.

    Provide GetValue an object to return if the key is not found:

    http://msdn2.microsoft.com/en-us/library/kk88y0s0.aspx

    this would pretty much give you an indication if the key in question exists, and if not, it will return back the object you gave it in its method parameter

    hope this helps in some way

     

    Friday, October 6, 2006 7:47 AM
  • Oh, thank you but can someone provide a line of code? I am kind of lost in reading the links above. I think I am missing something. Thanks.
    Saturday, October 7, 2006 3:31 AM
  • Try to modify false to be null

    if (Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run").GetValue("CA", "C:\\windows\\consoleapplication2.exe"); == null) //Check for Null not for false
    {
    Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true).SetValue("CA", "C:\\windows\\consoleapplication2.exe");
    }

    Best Regads,

    Saturday, October 7, 2006 10:03 AM