2010-05-26 12 views
8

Ehi, come può scorrere il registro usando C#? Vorrei creare una struttura per rappresentare gli attributi di ciascuna chiave.C# Come faccio a scorrere il registro?

+1

Questo romperà ogni portabilità con Mono su altri sistemi oltre a Windows. Solo un avvertimento – alternative

risposta

9

Penso che quello che ti serve sia GetSubKeyNames() come in questo esempio.

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

(NOTA: Questo è stato originale copiata da un tutorial http://www.csharphelp.com/2007/01/registry-ins-and-outs-using-c/, ma il sito ora sembra essere verso il basso).

+0

Grazie Chris! Stavo per scrivere una funzione arecursiva ma non ho familiarità con i metodi! Grazie – Tom

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/

Problemi correlati