I’m learning as I go with c# and I have been stuck with some code for a few weeks now and I’m hoping an experienced programmer can help.
Here is the scenario: When one connects to the remote computers’ registry you only get HKEY_LOCAL_MACHINE & HKEY_USERS. Now HKEY_USERS has a range of sub keys that related to the users profiles registry stored on the machine but is only displays it by numbers instead of Jo’s registry or John’s registry, hope you see where I’m going with this. So, using the code below provides one with the users profile name which is in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList and the sub key it’s stored in (distinguish by numbers) is the same sub key in HKEY_USERS…bingo.
Please note: This line of code if (!(sk.GetValue("Guid") == null)) ie.”Guid” is only present in a domain environment within the Profile list sub key and has a value called “Guid”. Because there are various Sub keys listed, I only want the ones which is a users profile. On my home machine I add the sub key in to make the code work.
using System;
using System.Windows.Forms;
using Microsoft.Win32;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//http://www.dreamincode.net/code/snippet1995.htm
//The registry key:
string SoftwareKey = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList";
using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(SoftwareKey))
{
foreach (string skName in rk.GetSubKeyNames())
{
using (RegistryKey sk = rk.OpenSubKey(skName))
{
try
{
if (!(sk.GetValue("Guid") == null))
{
string UserProfile;
string UserProfileComboBox;
string Software = null;
UserProfile = Software += sk.GetValue("ProfileImagePath");
UserProfileComboBox = UserProfile.Substring(9); //this reduces C:\Users\Home to Home. (0, 8) would have provide C:\Users
comboBox1.Items.Add(UserProfileComboBox);
//This messagebox displays the sub key I need when the userprofile is selected from the checkbox
MessageBox.Show(UserProfile + " " + skName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
}
}
In the above code I used: UserProfile.Substring(9) to reduce C:\Users\Home to just Home which is disable in the image below:

I’ve also added the following line of code to display the results to a message box for your understanding: MessageBox.Show(UserProfile + " " + skName);
What I would like to be able to do is…when I select the User profile from the combo box that the code knows which HKEY_USER subkey it relates too eg. S-1-5-21-340336367-1635450098-906894100-1008 (hkeyuserprofile) so I can do the registry key changes later:
CHANGE:
using (RegistryKey test = registry.Users.OpenSubKey(@"Software\Policies\Microsoft\..."))
To:
string hkeyuserprofile;
using (RegistryKey test = registry.Users.OpenSubKey(hkeyuserprofile + @"\Software\Policies\Microsoft\...")
Any help would be greatly appreciate! 😉
You can accomplish this in a couple of different ways. I’ll show you two, but the first is preferred over the second. (I realized after writing this the second way is not applicable, so I have refrained from adding it)
Databinding
The nice thing about most of the collection controls is that they have a fairly decent data-binding mechanism built into them. You can leverage this to bind the control to a collection of more strongly typed Objects for future retrieval.
First thing is first, you need an object that represents the information you want:
So now you have a type that will store both the derived name you want, and the SubKeyPath for later use.
Now we need to tell the combobox how it should handle this kind of object. We can set that up in the Form Constructor. Normally you could do this via the designer, but it’s easier to show you here.
Now you have told the
ComboBoxthat when you give it anObject, it should look for a property called “Name” and use that for the display, and use the property “SubKeyPath” for theValue.Instead of adding items manually to the
ComboBox, we are going to create a collection of ourRegKeyInfotype, and “Bind” it to theComboBox.The big difference here is making use of the
DataSourceproperty, which tells theComboBoxto use data-binding based on the information you have given it.When someone selects a value from the
ComboBox, you can access theSelectedValueproperty and retrive the SubKeyPath that it is bound to. Try adding in this event handler to theSelectedValueChangedevent to see what I’m talking about: