C#如何遍历注册表?

时间:2010-05-26 19:08:21

标签: c#

嘿,如何使用c#迭代注册表?我希望创建一个表示每个键的属性的结构。

4 个答案:

答案 0 :(得分:9)

我认为你需要的是GetSubKeyNames(),如本例所示。

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

(注意:这是从教程http://www.csharphelp.com/2007/01/registry-ins-and-outs-using-c/复制的原始文件,但网站现在似乎已关闭了。)

答案 1 :(得分:3)

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/

答案 2 :(得分:0)

从MSDN查看此功能: http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.getsubkeynames.aspx?PHPSESSID=ca9tbhkv7klmem4g3b2ru2q4d4

此函数将检索所有子项的名称,您可以遍历它们并执行任何操作。

答案 3 :(得分:0)

您可以使用此处所述的Microsoft.Win32.RegistryKeyGetSubKeyNames方法:

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

请注意,如果您在注册表的大部分内容进行迭代,这可能会非常慢。