我有一个 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/