注册表 class。如果存在价值

Registry class. If value exist

我正在使用 Microsoft。Win32.Registry class。我想做一个 if value exist 语句但不知道如何

我想要这样的东西:

Picture

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

如果我理解正确的话。

例如你可以这样做

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

对于注册表值,您可以获取当前键值的名称并检查此数组是否包含所需的值名称。

在你的代码中应该是这样的

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