2017-05-30 28 views
0

我有一本字典在哪裏存儲密鑰和它們的路徑,我想檢查關鍵在註冊表C#中存在

檢查註冊表中已經存在的路徑,這是我的詞典:

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"}, 
}; 

我試圖循環的字典,並嘗試在註冊表中的值和existant路徑之間進行比較,但我被困在這裏:

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

請出示你爲了得到從註冊表中的信息都試過了。示例問題:[在C#中查找註冊表項](https://stackoverflow.com/q/13417955/6400526) –

+0

嗨@GiladGreen,其實這就是我無法找到我正在考慮openSubKey,但我猜openSubKey只創建上午我錯了? – Code4Living

+0

不確定,但我的意思是,您請求幫助以檢查您的字典值是否存在於註冊表中,但是您的代碼未顯示您實際訪問註冊表。請嘗試從註冊表中獲取信息 –

回答

3

你可以寫一個簡單的方法來檢查是這樣的:

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 
    } 
} 
+0

非常感謝@RomanoZumbé這就是我一直在尋找 – Code4Living

+0

可能最好使用'使用(RegistryKey ret = baseKey.OpenSubKey(subKeyName))' –