Search code examples
c#if-statementtestingregistryregedit

Registry class. If key exist


Im using the Microsoft.Win32.Registry class. Im trying to make a if RegKey exist statement but don't know how

I want something like this:

RegistryKey key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\test");
if(key.keyExist("yourKey")) Console.WriteLine("yourKey exist!");

Solution

  • As far as I know, the SubKey is stored in a path in the system.

    So you can do something like this to check out if the SubKey exists:

    using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\test"))
    {
           if (key != null)
           {
                Console.WriteLine("yourKey exist!");
           }
           else
           {
               // e.g. create SubKey
           }
    }