Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How do i iterate through the registry?

Tags:

c#

Hey, how can iterate through the registry using c#? I wish to create a structure for representing attributes of each key.

like image 823
Tom Avatar asked May 26 '10 19:05

Tom


1 Answers

I think what you need is GetSubKeyNames() as in this example.

private void GetSubKeys(RegistryKey SubKey)
{
    foreach(string sub in SubKey.GetSubKeyNames())
    {
      MessageBox.Show(sub);
      RegistryKey local = Registry.Users;
      local = SubKey.OpenSubKey(sub,true);
      GetSubKeys(local); // By recalling itself it makes sure it get all the subkey names
    }
}

//This is how we call the recursive function GetSubKeys
RegistryKey OurKey = Registry.Users;
OurKey = OurKey.OpenSubKey(@".DEFAULT\test",true);
GetSubKeys(OurKey);

(NOTE: This was original copied from a tutorial http://www.csharphelp.com/2007/01/registry-ins-and-outs-using-c/, but the site now appears to be down).

like image 95
ChrisF Avatar answered Oct 11 '22 13:10

ChrisF