c# - 检查注册表中是否存在 key

标签 c# registry

我有一个 Dictionary<sting,string>我在其中存储 key 及其路径,我想检查这些路径是否已存在于注册表中。

那是我的字典:

public static Dictionary<string, string> AllRegKeys = new Dictionary<string,string>()
{
    {"clientId", "MyApp\\credentials\\Identif"},
    {"clientSecret", "MyApp\\credentials\\Identif"},
    {"Key 1", "MyApp\\credentials\\Identif"},
    {"key 2 ", "MyApp\\credentials\\Identif"},
    {"using link ", "MyApp\\credentials\\Folder"},
    {"category", "MyApp\\credentials\\Folder\\Cat"},
    {"link1", "MyApp\\credentials\\Settings\\link"},
    {"link2", "MyApp\\credentials\\Settings\\link"},
    {"link3", "MyApp\\credentials\\Settings\\link"},
};

我试图循环字典并尝试比较注册表中的值和现有路径,但我被困在这里:

foreach (KeyValuePair<string, string> entry in ConstantsField.AllRegKeys)
{
    if(entry.Value== )
}

最佳答案

你可以像这样写一个简单的方法来检查:

private bool KeyExists(RegistryKey baseKey, string subKeyName)
{
    RegistryKey ret = baseKey.OpenSubKey(subKeyName);

    return ret != null;
}

然后你可以这样调用它:

foreach (KeyValuePair<string, string> entry in ConstantsField.AllRegKeys)
{ 
    //adjust the baseKey
    if(KeyExists(Registry.LocalMachine, $"{entry.Value}\\{entry.key}")
    {
          //do something
    }
}

关于c# - 检查注册表中是否存在 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44258960/

相关文章:

c# - 如何用用户输入替换文本文件中一行的一部分?

c# - 用户更改系统文化时如何接收事件

c# - MVC 模型绑定(bind)到列表 - 仅适用于列表中的第一项

Windows shell 在单击文件夹的空白部分时将项目添加到上下文菜单

.net - 注册为在客户端计算机上注册的 Excel 编写的 COM 服务器时出现问题(无法设置 mscoree.dll 的完整路径)

C#无限迭代

c# - 如何在 Controller 操作中访问 $orderBy 等查询参数?

Wix - 根据条件安装目录

windows - 在 Windows 注册表项 Shell\Open\Command 中指定当前目录

rust - 如何正确处理来自Winreg crate 的Regvaule类型?