首页 > 解决方案 > 注册表类。如果值存在

问题描述

我正在使用 Microsoft.Win32.Registry 类。我试图做一个 if value exists 语句,但不知道如何

我想要这样的东西:

图片

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

标签: c#classif-statementregistryregedit

解决方案


如果我理解正确的话。

例如你可以做这样的事情

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

推荐阅读