Using the Windows Registry

The most important things you need to do with the registry - creating, accessing editing and deleting SubKeys and values.

I suppose you already know the basics of using the Windows registry, adding a value or modifying it using the Registry Editor (regedit). Before we start… don’t forget to backup! 

Start a new Console Application project in Microsoft Visual C# .NET.

We will use the Microsoft.Win32.Registry and Microsoft.Win32.RegistryKey classes, therefore add the following line so we don’t have to write so much when we shall use this namespace:


using Microsoft.Win32;

Creating SubKeys in the registry

We create a subkey by using the CreateSubKey method. We usually want to create a subkey in HKEY_CURRENT_USER/SOFTWARE that is why we use the following code:


RegistryKey reg = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Geekpedia\\Test");

Adding values to SubKeys

Now we can add a value to the subkey by using the following piece of code:


reg.SetValue("URL", "http://www.geekpedia.com");



As you can see, the code is rather straightforward. We create a value with the name ‘URL’ and the data ‘http://www.geekpedia.com’.

Retrieving data

Further, we shall retrieve the value we just added and store it in a string variable:


string URL = (string)reg.GetValue("URL");



…and display the output:


Console.WriteLine("Before: " + URL);

Changing the values

We change the value of ‘reg’. We specify the same path, but now we also add a ‘true’ boolean at the end. That means we use the overloaded method and ‘true’ means we want the path in write mode.
The overloaded method prototype is:


public RegistryKey OpenSubKey(string name, bool writable);



And here is the code we use:


RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Geekpedia\\Test", true);



Because we opened the SubKey in write mode, we can modify the data:


reg.SetValue("URL", "about:blank");



and store it again in the same variable:


URL = (string)reg.GetValue("URL");



…and display the output:


Console.WriteLine("After: " + URL);



And finally close the opened connection with the registry SubKey and flush:


reg.Close();



Finally, all the code put together looks like this:


using System;
using Microsoft.Win32;
class Class1
{
    [STAThread]
    static void Main(string[] args)
    {
        // Create a new SubKey or open it if already exists
        RegistryKey reg = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Geekpedia\\Test");
        // Add a new value to the SubKey
        reg.SetValue("URL", "http://www.geekpedia.com");
        // Store it in 'URL' variable
        string URL = (string)reg.GetValue("URL");
        // Display it
        Console.WriteLine("Before: " + URL);

        // Open the SubKey this time in write mode
        reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Geekpedia\\Test", true);
        // Change the value 'URL'
        reg.SetValue("URL", "about:blank");
        // Store it in the earlier variable
        URL = (string)reg.GetValue("URL");
        // Display it
        Console.WriteLine("After: " + URL);
        // Close the registry SubKey and flush
        reg.Close();
    }
}

Deleting SubKeys

For removing SubKeys from the registry you can use the method with the following prototype:


public void DeleteSubKey(string subkey);



And here is a hands-on example:


// Open SOFTWARE
RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE");
// Delete Geekpedia from SOFTWARE
reg.DeleteSubKeyTree("Geekpedia");
//Close the registry SubKey and flush
reg.Close();



If the key doesn’t exist you will get a nasty exception. For ignoring the exception use the overloaded method:


public void DeleteSubKey(string subkey, bool throwOnMissingSubKey);



In our example this looks like this:


reg = Registry.CurrentUser.OpenSubKey("SOFTWARE", false);

Deleting Values

Deleting a value is similar to deleting a SubKey, it also has two prototypes:


public void DeleteValue(string name);
public void DeleteValue(string name, bool throwOnMissingValue);

Which act the same as in DeleteSubKey.

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top