Search code examples
c#classif-statementregistryregedit

Registry class. If value exist


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

I want something like this:

Picture

private RegistryKey key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Test");
if(key.ValueExist("myValue")) Console.WriteLine("value exist!");

Solution

  • If i understood you properly.

    For example you can do something like this

    public static bool checkMachineType()
    {    
        RegistryKey key = Registry.LocalMachine.OpenSubKey(@"System\Set\services\something", true);
        return (key.GetValueNames().Contains("value"));
    }
    

    For Registry Value you can get names of Values for the current key and check if this array contains the needed Value name.

    In your code this should be like this

    private RegistryKey key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Test");
    
    RegistryKey getKey= Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Test", true);
    if(getKey.GetValueNames().Contains("value")) 
    {
      Console.WriteLine("value exist!");
    }