How to check if a registry value exists using C#?

后端 未结 7 1502
庸人自扰
庸人自扰 2020-12-08 13:28

How to check if a registry value exists by C# code? This is my code, I want to check if \'Start\' exists.

public static bool checkMachineType()
{
    Registr         


        
7条回答
  •  -上瘾入骨i
    2020-12-08 13:41

    public bool ValueExists(RegistryKey Key, string Value)
    {
       try
       {
           return Key.GetValue(Value) != null;
       }
       catch
       {
           return false;
       }
    }
    

    This simple function will return true only if a value is found but it is not null, else will return false if the value exists but it is null or the value doesn't exists in the key.


    USAGE for your question:

    if (ValueExists(winLogonKey, "Start")
    {
        // The values exists
    }
    else
    {
        // The values does not exists
    }
    

提交回复
热议问题