开发者

C# How do i iterate through the registry?

Hey, how can iterate through the registry using c#? I wish to create a structure for representing attribut开发者_如何学Goes of each key.


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).


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 itselfit 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);

http://www.csharphelp.com/2007/01/registry-ins-and-outs-using-c/


Check out this function from MSDN: http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.getsubkeynames.aspx?PHPSESSID=ca9tbhkv7klmem4g3b2ru2q4d4

This function will retrieve the name of all subkeys and you can iterate through them and do whatever you wish.


You can use Microsoft.Win32.RegistryKey and the GetSubKeyNames method as described here:

http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey_members%28v=VS.100%29.aspx

Be aware though that this might be very slow if you're iterating through a large part of the registry.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜