I need to loop through the registry and get all subkeys and the all values.
This it's what I been trying (to get all subkeys only):
public void OutputRegKey(RegistryKey Key)
{
foreach (string keyname in Key.GetSubKeyNames())
{
try
{
using (RegistryKey key2 = Key.OpenSubKey(keyname))
{
foreach (string valuename in Key.GetValueNames())
{
comboBox1.Items.Add(valuename);
OutputRegKey(key2);
}
}
}
catch
{
}
}
}
How do I get all the values? I need to do two methods: one that gets all the subkeys and other that gets all the values. I'm only asking the one to get all the values. Thank you.
PS: It's not homework or otherwise related to academics. It's something personal.
Once given the RegistryKey, you could easily get the key's ValueNames by Key.GetValueNames() and to get each of the individual key's Value for each ValueName by Key.GetValue(valueName) which will return an object.
Borrowing your code to elaborate, here is how it should be modified to,
private void processValueNames(RegistryKey Key) { //function to process the valueNames for a given key
string[] valuenames = Key.GetValueNames();
if (valuenames == null || valuenames.Length <= 0) //has no values
return;
foreach (string valuename in valuenames) {
object obj = Key.GetValue(valuename);
if (obj != null)
comboBox1.Items.Add(Key.Name + " " + valuename + " " + obj.ToString()); //assuming the output to be in comboBox1 in string type
}
}
public void OutputRegKey(RegistryKey Key) {
try {
string[] subkeynames = Key.GetSubKeyNames(); //means deeper folder
if (subkeynames == null || subkeynames.Length <= 0) { //has no more subkey, process
processValueNames(Key);
return;
}
foreach (string keyname in subkeynames) { //has subkeys, go deeper
using (RegistryKey key2 = Key.OpenSubKey(keyname))
OutputRegKey(key2);
}
processValueNames(Key);
} catch (Exception e) {
//error, do something
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With