c# - 注册表类。如果值存在
问题描述
我正在使用 Microsoft.Win32.Registry 类。我试图做一个 if value exists 语句,但不知道如何
我想要这样的东西:
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!");
}
推荐阅读
- label - 如何自定义等高线标签?
- c++ - 如何用整数填充数组并显示它?
- jquery - 删除项目后如何禁用可拖动?
- mysql - 在 Django 中将未知对象类型保存到数据库中
- java - 使用 Spring 创建 MapStruct bean 时出错
- css - Bootstrap 4 导航栏在点击图标时关闭
- prolog - Prolog 中的“isDuped”谓词
- android - 如何在使用表 B 凭据时从表 A 中查询数据?
- python-3.x - 关于 Sublime Text3 中的 isort 插件?
- html - 使用不带参数的 mixin 是否比使用 extend 更有效