Registry In's and Out's Using C#

The RegistryKey class

The RegistryKey class is the most important part in terms of registry manipulation when using C#. The RegistryKey class has many methods which the use can exploit to create and delete data. Before we can do any of this we have to create an Instance of a Registry Key, this is done using the code below:

RegistryKey OurKey = Registry.Users;

In the above code extract we have created a instance of RegistryKey called OurKey, we have initialized this key to be the HKEY_USERS subkey in the registry. Basically this mean that any method we use in this key will effect the direct subkeys of HKEY_USERS and their data items.


OpenSubkey is a very important method when using the RegistryKey class, because it allows us to access/manipulate the subkey of the top key. This may sound a tad strange, basically if as above we have "OurKey" set to the HKEY_USERS key, using the OpenSubKey method we can set "OurKey" to a subkey of HKEY_USERS. The best we to demonstrate this is to show you some code (See below).

RegistryKey OurKey = Registry.Users;
OurKey = OurKey.OpenSubKey(".DEFAULT", true );

In the above code sample we have created our instance of the RegistryKey class and given it the value of the HKEY_USERS key. The next step is that we have opened a subkey within HKEY_USERS called ".DEFAULT", we should also note that it doesn't have to be a top level subnode either, We could have opened ".DEFAULT\subkey". The second part to the method is defining if the key is opened and Read-only mode or Read/Write mode. If the value is true we can edit items within the key, and the key itself.

DeleteSubKey() / CreateSubKey() & DeleteSubKeyTree()

All these three methods are to do with the management of Subkeys under the current selected key. These methods are pretty self explanatory and the below code shows there implementation.

RegistryKey OurKey = Registry.Users; // Create OurKey set to HKEY_USERS
OurKey = OurKey.OpenSubKey(".DEFAULT", true ); // Set it to HKEY_USERS\.DEFUALT
OurKey.CreateSubKey("OurSubKey"); // Create the key HKEY_USERS\.DEAFULT\OurSubKey
OurKey.CreateSubKey(@"OurSubKey\Subkey"); // Create a sub key HKEY_USERS\.DEFAULT\OurSubKey\Subkey
OurKey.DeleteSubKey(@"OurSubKey\SubKey"); // Delete the subkey name "subkey"
OurKey.DeleteSubKeyTree("OurSubKey"); // Delete the whole subkey and any subkeys below it


GetSubKeyNames is an important method because it allows us to get all the names of the secondary subkeys, For instance we could get all the names of the subkeys below "HKEY_USERS". The only draw back being that it only gets the immediate subkey names. However with a little recursion we can get them all, the first code snippet is of the basic use of GetSubKeyNames().

//The first example shows it using a foreach loop to display each subkeyname
foreach ( string Keyname in OurKey.GetSubKeyNames())
//The second example shows how to tranfer the names into a string array
string[] Keynames = OurKey.GetSubKeyNames();

In the next snippet of code we see how we can use recursion to obtain all the names of the subkeys.

private void GetSubKeys(RegistryKey SubKey)
   foreach ( string sub in SubKey.GetSubKeyNames())
       RegistryKey local = Registry.Users;
       local = SubKey.OpenSubKey(sub, true );
       GetSubKeys(local); // By recalling itself it makes sure it get all the subkey names
//This is how we call the recursive function GetSubKeys
RegistryKey OurKey = Registry.Users;
OurKey = OurKey.OpenSubKey(@".DEFAULT\test", true );

You might also like...



Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Every language has an optimization operator. In C++ that operator is ‘//’”