Help with registry, array, and listbox

  • Thread starter Thread starter vbMark
  • Start date Start date
V

vbMark

Hello,

In regards to the Registry...

Can someone give me an example of how to list an unknown number of subkeys
under a key, and put the key names in one column of a listbox, and the key
values in another column of a listbox?

Thanks!
 
vbMark said:
Hello,

In regards to the Registry...

Can someone give me an example of how to list an unknown number of subkeys
under a key, and put the key names in one column of a listbox, and the key
values in another column of a listbox?

Thanks!

Look at GetSubKeyNames(), and GetValueNames of the RegistryKey object.

HTH,
Eric
 
Look at GetSubKeyNames(), and GetValueNames of the RegistryKey object.

HTH,
Eric

I know this isn't right but I know I'm close.
What do I need to do to fix this?

string[] strKeys;
string[] strValues;
RegistryKey rk = Registry.CurrentUser;
rk = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion
\\Internet Settings");
strKeys = rk.GetSubKeyNames();
for (int i = 0; i < strKeys.Length; i++)
rk = strKeys.GetValue(i);
listBox.Items.Add(rk.GetValueNames);
 
vbMark said:
I know this isn't right but I know I'm close.
What do I need to do to fix this?

string[] strKeys;
string[] strValues;
RegistryKey rk = Registry.CurrentUser;
rk = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion
\\Internet Settings");
strKeys = rk.GetSubKeyNames();
for (int i = 0; i < strKeys.Length; i++)
rk = strKeys.GetValue(i);
listBox.Items.Add(rk.GetValueNames);

You are getting closer. One thing is, I'm not really sure what you're trying
to show to the user, so it's hard to be more helpful. But for starters, you
should understand that a key can hold subkeys, which can hold subkeys, etc,
etc; which at the bottom hold name value pairs. So not all keys will have a
single value which means anything if you can figure out a way to display it
in one line of a list box. In other words the depth of the registry is
unknown (at least to me.)

Now, your last try in code. You wrote
RegistryKey rk = Registry.CurrentUser;
rk = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion
\\Internet Settings");

You're setting rk twice in two lines. Not necessary. (watch word wrap)

RegistryKey rk =
Registry.CurrentUser.OpenSubKey(@"Software\\Microsoft\\Windows\\CurrentVersi
on\\Internet Settings");
Console.WriteLine("Subkeys: " + rk.SubKeyCount); // = 10 on my machine
Console.WriteLine("Values: " + rk.ValueCount); //= 30 on my machine

then you have
strKeys = rk.GetSubKeyNames();
which returns all the subkeys which are like folders, and which may be what
you want, but I'm not sure. The names you get are other keys, which can
contain values and other keys. So I'm not sure what you get with the for
loop but it's probably not what you want.

On my machine, the key you open has ten subkeys, and 30 values, which are
different. The subkeys are like subfolders, and the values like files.

Once you figure out which it is you want, the subkeys or the values for a
key, you can load the list box using just two lines like
string[] keys = rs.GetSubKeyNames();
listBox1.DataSource = keys;

HTH
Eric
 
Back
Top