A Registry Viewer in C#

The Registry is a database in Windows that is available for application developers to use for storing small amounts of data. It is mostly used to save application setting like the window size and database connection string. In this series of post I will demonstrate how to create a simple registry editor in C#.
The Registry consists of 5 root keys. Each root key has sub keys that are organized in a CompanyName\SoftwareName\VersionNumber hierarchy. And in each sub key a value could be inserted with some data in it. The root keys and their .NET classes are:
- HKEY_CLASSES_ROOT (ClassesRoot)
- HKEY_CURRENT_CONFIG (CurrentConfig)
- HKEY_CURRENT_USER (CurrentUser)
- HKEY_LOCAL_MACHINE (LocalMachine)
- HKEY_USERS (Users)
The 2 classes needed to work with the Registry are Microsoft.Win32.RegistryKey and Microsoft.Win32.Registry, they could be used to manipulate most of the sub keys, but all of root keys are read only. The Microsoft.Win32.RegistryKey object is used to manage the sub keys, and the Microsoft.Win32.Registry is used to read the root keys. The following code reads the entire first and second level of the sub keys in all the root keys that are available to the current user:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
private void ReadRegistry()
{
//HKEY_CLASSES_ROOT
TreeNode rootNode = new TreeNode(Registry.ClassesRoot.Name, 0, 1);
string[] rootSubKeys = Registry.ClassesRoot.GetSubKeyNames();
foreach (string key in rootSubKeys)
{
TreeNode node = new TreeNode(key, 0, 1);
string[] subKeys = Registry.ClassesRoot.OpenSubKey(key).GetSubKeyNames();
foreach (string subKeysKey in subKeys)
{
node.Nodes.Add(subKeysKey, subKeysKey, 0, 1);
}
rootNode.Nodes.Add(node);
}
registryTreeView.Nodes.Add(rootNode);
//HKEY_CURRENT_CONFIG
TreeNode configNode = new TreeNode(Registry.CurrentConfig.Name, 0, 1);
string[] configSubKeys = Registry.CurrentConfig.GetSubKeyNames();
foreach (string key in configSubKeys)
{
TreeNode node = new TreeNode(key, 0, 1);
string[] subKeys =
Registry.CurrentConfig.OpenSubKey(key).GetSubKeyNames();
foreach (string subKeysKey in subKeys)
node.Nodes.Add(subKeysKey, subKeysKey, 0, 1);
configNode.Nodes.Add(node);
}
registryTreeView.Nodes.Add(configNode);
//HKEY_CURRENT_USER
TreeNode currentUserNode = new TreeNode(Registry.CurrentUser.Name, 0, 1);
string[] currentUserSubKeys = Registry.CurrentUser.GetSubKeyNames();
foreach (string key in currentUserSubKeys)
{
TreeNode node = new TreeNode(key, 0, 1);
string[] subKeys = Registry.CurrentUser.OpenSubKey(key).GetSubKeyNames();
foreach (string subKeysKey in subKeys)
node.Nodes.Add(subKeysKey, subKeysKey, 0, 1);
currentUserNode.Nodes.Add(node);
}
registryTreeView.Nodes.Add(currentUserNode);
//HKEY_LOCAL_MACHINE
TreeNode localMachineNode = new TreeNode(Registry.LocalMachine.Name);
string[] localMachineSubKeys = Registry.LocalMachine.GetSubKeyNames();
foreach (string key in localMachineSubKeys)
{
TreeNode node = new TreeNode(key, 0, 1);
try
{
string[] subKeys =
Registry.LocalMachine.OpenSubKey(key, false).GetSubKeyNames();
foreach (string subKeysKey in subKeys)
node.Nodes.Add(subKeysKey, subKeysKey, 0, 1);
}
catch (Exception)
{
//an exception is thrown if the user has no access to this subkey
//if this is the case, change the icon to show a dimmed folder
node.ImageIndex = 4;
node.SelectedImageIndex = 5;
}
localMachineNode.Nodes.Add(node);
}
registryTreeView.Nodes.Add(localMachineNode);
//HKEY_USERS
TreeNode usersNode = new TreeNode(Registry.Users.Name);
string[] usersSubKeys = Registry.Users.GetSubKeyNames();
foreach (string key in usersSubKeys)
{
TreeNode node = new TreeNode(key, 0, 1);
try
{
string[] subKeys = Registry.Users.OpenSubKey(key).GetSubKeyNames();
foreach (string subKeysKey in subKeys)
node.Nodes.Add(subKeysKey, subKeysKey, 0, 1);
}
catch (Exception)
{
//an exception is thrown if the user has no access to this subkey
//if this is the case, change the icon to show a dimmed folder
node.ImageIndex = 4;
node.SelectedImageIndex = 5;
}
usersNode.Nodes.Add(node);
}
registryTreeView.Nodes.Add(usersNode);
} |
The fastest way to read the values and data in a sub key is to use the GetValueNames and GetValue methods of the RegistyKey object. The GetValuesAndData function below takes 2 arguments, a RegistryKey object and a TreeNode object. It fills the TreeNode object with values in the RegistryKey object in a ‘Value: Data’ format. The data is also truncated if it is greater than 50 characters.
The array of strings called values is set to the list of Registry values in the sub key, and a foreach loops through these values. In each iteration of the loop the data of the current value is retrieved using the GetValue function of the RegistryKey object.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
private static void GetValuesAndData(RegistryKey registryKey, TreeNode node)
{
string[] values = registryKey.GetValueNames();
foreach (string value in values)
{
object data = registryKey.GetValue(value);
if (data != null)
{
string stringData = data.ToString();
//if the data is too long, display the begining only
if (stringData.Length > 50)
stringData = stringData.Substring(0, 46) + " ...";
//Display the data of the value. The conditional operatore is
//needed because the default value has no name
node.Nodes.Add(value, (value == "" ? "Default" : value) +
": " + stringData, 2, 2);
}
else
{
//Display <empty> if the value is empty
node.Nodes.Add(value, (value == "" ? "Default" : value) +
": <empty>", 2, 2);
}
}
} |
Download Source Code
Related Posts:
Comments (8)
Links to this Post
- DotNetShoutout | March 29, 2009






How would i check to see if the data is in a default value?
if default value dosnt have any data ok if not would you like to remove it.
http://www.summeylabs.com/images/reg.jpg
thats what im trying to check
my if statement dosnt work if i have data in that value it still say its empty etc…
thank you for your time.
below is my code.
static void Main(string[] args)
{
RegistryKey MyReg = Registry.LocalMachine.OpenSubKey
(“SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\setup.exe”, true);
string IsVal = (string)MyReg.GetValue(“Default”,”");
if (MyReg.GetValue(IsVal) != null)
{
Console.WriteLine(“empty”);
MyReg.Flush();
MyReg.Close();
}
else
{
Console.WriteLine(MyReg.GetValue(IsVal).ToString());
Console.WriteLine(“removing value now”);
MyReg.SetValue(“”,”DefaultValue”);
MyReg.Flush();
MyReg.Close();
}
Hi Ryan, a value in registry terms means the variable name. And the default values in a registry key are the ones without names. I checked for default variables in the function GetValuesAndData using the following code:
//Display the data of the value. The conditional operatore is
//needed because the default value has no name
node.Nodes.Add(value, (value == “” ? “Default” : value) + “: ” + stringData, 2, 2);
This just displays the word Default when the value has no name. I hope this answered your question.
Iv got my code working after a few hours of tinkering i found that when i set the (default) to “” it doesn’t change it truly to null. if you look in the binary it has some zeros in there when its truly null it says (value not set) in the data field and if you check the binary it will only have 0000 so my question is how would you delete or set the default values data to (value not set ) making it null?
thank you for your time really appreciate it.
-Summey
Ryan I think this would do the trick:
RegistryKey demoKey = Registry.CurrentUser.OpenSubKey(“CodeHill”);
demoKey.SetValue(“TestValue”, 0, RegistryValueKind.DWord);
Thanks for the article.
If have made a view changes to get to all levels.
private void UpdateTree()
{
TreeNode currentUserNode = new TreeNode(Registry.CurrentUser.Name, 0, 1);
string[] currentUserSubKeys = Registry.CurrentUser.GetSubKeyNames();
this.ReadRegistry(currentUserNode, “”, currentUserSubKeys);
registryTreeView.Nodes.Add(currentUserNode);
}
private void ReadRegistry(TreeNode Node, string Key, string[] SubKeys)
{
string TotaalPad = Key;
if (TotaalPad.Length > 0)
TotaalPad += @”\”;
foreach (string key in SubKeys)
{
//get a list of the next level sub keys and their values
TreeNode node = new TreeNode(key, 0, 1);
string[] subKeys = new string[0];
try
{
subKeys = Registry.CurrentUser.OpenSubKey(TotaalPad + key).GetSubKeyNames();
}
catch
{
}
if (subKeys.Length > 0)
{
this.label1.Text = TotaalPad + key + ” subKeys: ” + subKeys.Length.ToString();
this.label1.Update();
this.ReadRegistry(node, TotaalPad + key, subKeys);
}
try
{
GetValuesAndData(Registry.CurrentUser.OpenSubKey(TotaalPad + key), node);
}
catch
{
}
node.Name = node.Text;
Node.Nodes.Add(node);
}
}
I’am considering writing my own registry editor, but mostly for remote machines… I will only be accessing HKEY_LOCAL_MACHINE and HKEY_USERS and their sub-keys.
Can you offer any guidance? This is for a management tool for my companies internal use only – so far I have not been able to figure out how to get regedit.exe to automatically connect to a remote registry with the hostname supplied on the command line. If there is already a freeware product that does this, that would be great, else I’m stuck with writing one.
Thanks,
Lee Parker
@Lee Parker, I think you can find tools that can edit a remote PC’s registry in the following links:
http://www.brothersoft.com/downloads/remote-registry-editor.html
http://3d2f.com/tags/remote/registry/editor/
and maybe download.com too. If you decided to develop your own then my advice is to write the code that read and write to the registry in an app that accepts TCP connections and install it in your client PCs. And develop another app that sends the text to it from your PC. This way you dont have to try and manipulate the registry remotely and deal with a lot of networking issues. I hope that helps.