2

I have a Dictionary<sting,string> where I store keys and their paths and I want to check if those paths exist already in the Registry.

That's my dictionary:

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

I've tried to loop on the dictionary and try to compare between the values and existing paths in the Registry but i'm stuck in here:

foreach (KeyValuePair<string, string> entry in ConstantsField.AllRegKeys)
{
    if(entry.Value== )
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Code4Living
  • 35
  • 1
  • 8
  • 5
    Please show what you have tried in order to get the information from the registry. Example question: [Finding Registry Keys in C#](https://stackoverflow.com/q/13417955/6400526) – Gilad Green May 30 '17 at 09:53
  • Hi @GiladGreen , actually that's what i couldn't find i was thinking about openSubKey but i guess openSubKey only creates am i wrong ? – Code4Living May 30 '17 at 10:01
  • Not sure but what I meant is that you request help for checking if your values from the dictionary are present in the registry but your code doesn't show that you are actually accessing the registry. Please show an attempt with trying to get the information from the registry – Gilad Green May 30 '17 at 10:03

1 Answers1

7

You could write a simple method to check like this:

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

    return ret != null;
}

You can then call it like this:

foreach (KeyValuePair<string, string> entry in ConstantsField.AllRegKeys)
{ 
    //adjust the baseKey
    if(KeyExists(Registry.LocalMachine, $"{entry.Value}\\{entry.key}")
    {
          //do something
    }
}
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55