Iterate through registry sub keys.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to read values to the subkeys under HKLM\...\Uninstall.

I get to this point...

RegistryKey Sftw = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine,
name).OpenSubKey("software").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Uninstall");

foreach (string SubKey in Sftw.GetSubKeyNames())
{

}

and now I want to read some of the values to the subkeys. Nothing I have
tried works. All I have been able to accomplish is reading the name of the
subkey itself.

I feel I am missing something here...

Thanks for any assistance!
 
James,

You say it doesn't work. Are you sure you have permission to access
those keys? Also, do you get an exception, or it doesn't return the value
that you think you should get?
 
I'm guessing I just don't know the method to read the subkeys. The only
value I have been able to figure out how to return is the name of the
subkeys... I did try this...

foreach (RegKey SubKey in Sftw.GetSubKeyNames())
{
Console.WriteLine(SubKey.GetValue("DisplayName").ToString());
}

and caught an error converting string to registrykey.

Nicholas Paldino said:
James,

You say it doesn't work. Are you sure you have permission to access
those keys? Also, do you get an exception, or it doesn't return the value
that you think you should get?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

James said:
I am trying to read values to the subkeys under HKLM\...\Uninstall.

I get to this point...

RegistryKey Sftw =
RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine,
name).OpenSubKey("software").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Uninstall");

foreach (string SubKey in Sftw.GetSubKeyNames())
{

}

and now I want to read some of the values to the subkeys. Nothing I have
tried works. All I have been able to accomplish is reading the name of
the
subkey itself.

I feel I am missing something here...

Thanks for any assistance!
 
James,

I see it now. You will have to open each sub key, you can't just cast
it to a registry key. You need to do this:

foreach (string subKeyName in Sftw.GetSubKeyNames())
{
// Open the key.
using (RegistryKey subKey = Sftw.OpenSubKey(subKeyName))
{
// Write the value.
Console.WriteLine(subKey.GetValue("DisplayValue").ToString());
}
}


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

James said:
I'm guessing I just don't know the method to read the subkeys. The only
value I have been able to figure out how to return is the name of the
subkeys... I did try this...

foreach (RegKey SubKey in Sftw.GetSubKeyNames())
{
Console.WriteLine(SubKey.GetValue("DisplayName").ToString());
}

and caught an error converting string to registrykey.

Nicholas Paldino said:
James,

You say it doesn't work. Are you sure you have permission to access
those keys? Also, do you get an exception, or it doesn't return the
value
that you think you should get?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

James said:
I am trying to read values to the subkeys under HKLM\...\Uninstall.

I get to this point...

RegistryKey Sftw =
RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine,
name).OpenSubKey("software").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Uninstall");

foreach (string SubKey in Sftw.GetSubKeyNames())
{

}

and now I want to read some of the values to the subkeys. Nothing I
have
tried works. All I have been able to accomplish is reading the name of
the
subkey itself.

I feel I am missing something here...

Thanks for any assistance!
 
That made the difference... I saw the keyword using in the MSDN examples I
just could not see how to apply it here. Thanks for your help!

Nicholas Paldino said:
James,

I see it now. You will have to open each sub key, you can't just cast
it to a registry key. You need to do this:

foreach (string subKeyName in Sftw.GetSubKeyNames())
{
// Open the key.
using (RegistryKey subKey = Sftw.OpenSubKey(subKeyName))
{
// Write the value.
Console.WriteLine(subKey.GetValue("DisplayValue").ToString());
}
}


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

James said:
I'm guessing I just don't know the method to read the subkeys. The only
value I have been able to figure out how to return is the name of the
subkeys... I did try this...

foreach (RegKey SubKey in Sftw.GetSubKeyNames())
{
Console.WriteLine(SubKey.GetValue("DisplayName").ToString());
}

and caught an error converting string to registrykey.

Nicholas Paldino said:
James,

You say it doesn't work. Are you sure you have permission to access
those keys? Also, do you get an exception, or it doesn't return the
value
that you think you should get?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I am trying to read values to the subkeys under HKLM\...\Uninstall.

I get to this point...

RegistryKey Sftw =
RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine,
name).OpenSubKey("software").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Uninstall");

foreach (string SubKey in Sftw.GetSubKeyNames())
{

}

and now I want to read some of the values to the subkeys. Nothing I
have
tried works. All I have been able to accomplish is reading the name of
the
subkey itself.

I feel I am missing something here...

Thanks for any assistance!
 
RegistryKey Sftw = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine,
name).OpenSubKey("software").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Uninstall");

FYI you don't have to open every key in the path, you can directly
open just the Uninstall key. A better way would be

using (RegistryKey Sftw =
RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine,
name).OpenSubKey("software\\Microsoft\\Windows\CurrentVersion\\Uninstall"))



Mattias
 
Back
Top