Can a selected item from a ComboBox provide a string from a registry key to use as a variable?
I'm learning as I go wit开发者_JAVA百科h 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:
public class RegKeyInfo
{
public String SubKeyPath { get; set; }
public String Name { get; set; }
}
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.
public Form1()
{
InitializeComponent();
comboBox1.ValueMember = "SubKeyPath";
comboBox1.DisplayMember = "Name";
}
Now you have told the ComboBox
that when you give it an Object
, it should look for a property called "Name" and use that for the display, and use the property "SubKeyPath" for the Value
.
Instead of adding items manually to the ComboBox
, we are going to create a collection of our RegKeyInfo
type, and "Bind" it to the ComboBox
.
private void Form1_Load(object sender, EventArgs e)
{
//http://www.dreamincode.net/code/snippet1995.htm
//Declare the string to hold the list:
var keys = new List<RegKey>();
//Snip...
UserProfileComboBox = UserProfile.Substring(9);
keys.Add(new RegKey {
Name = UserProfileComboBox,
KeyPath = skName
});
//Snip...
comboBox1.DataSource = keys;
}
The big difference here is making use of the DataSource
property, which tells the ComboBox
to use data-binding based on the information you have given it.
When someone selects a value from the ComboBox
, you can access the SelectedValue
property and retrive the SubKeyPath that it is bound to. Try adding in this event handler to the SelectedValueChanged
event to see what I'm talking about:
void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
MessageBox.Show(comboBox1.SelectedValue as String);
}
//Updated code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
namespace RegistryUSER
{
public partial class Form1 : Form
{
public class RegKeyInfo
{
public String SubKeyPath { get; set; }
public String Name { get; set; }
}
public Form1()
{
InitializeComponent();
comboBox1.ValueMember = "SubKeyPath";
comboBox1.DisplayMember = "Name";
}
private void Form1_Load(object sender, EventArgs e)
{
var keys = new List<RegKeyInfo>();
using (RegistryKey ProfileKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"))
{
foreach (string skName in ProfileKey.GetSubKeyNames())
{
using (RegistryKey sk = ProfileKey.OpenSubKey(skName))
{
try
{
if (!(sk.GetValue("Guid") == null))
{
string UserProfile;
string UserProfileKey;
string ProfileValue = null;
string ProfileKeyName = null;
UserProfile = ProfileValue += sk.GetValue("ProfileImagePath");
UserProfileKey = ProfileKeyName += skName;
RegKeyInfo UserInformation = new RegKeyInfo();
UserInformation.Name = UserProfile;
UserInformation.SubKeyPath = UserProfileKey;
UserProfile = UserProfile.Substring(9); //this reduces C:\Users\Home to Home. (0, 8) would have provide C:\Users
keys.Add(new RegKeyInfo
{
Name = UserProfile,
SubKeyPath = UserProfileKey
});
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
comboBox1.DataSource = keys;
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string result = comboBox1.SelectedValue as string;
using (RegistryKey Key = Registry.Users.OpenSubKey(result + @"\Software\Microsoft\Office\14.0\Outlook\OST"))
if (Key != null)
{
int Value = System.Convert.ToInt32(Key.GetValue("NoOST"));
if (Value == 3)
{
MessageBox.Show("yes");
}
else
{
MessageBox.Show("no");
}
}
else
{
MessageBox.Show("not present");
}
}
catch (Exception)
{
throw;
}
}
}
}
精彩评论