2010-05-26 25 views
8

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

+1

这将打破使用Mono任何便携性上的其他系统除了Windows之外。只是一个警告。 – alternative 2010-05-26 19:15:42

回答

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/复制,但该网站现在似乎已关闭)。

+0

谢谢克里斯!我正要编写递归函数,但我不熟悉这些方法! 谢谢 – Tom 2010-05-26 19:23:10

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/