How to access the Windows Registry in C#

By FoxLearn 1/7/2025 3:13:01 AM   87
Microsoft .NET allows you to interact with the Windows Registry programmatically, enabling you to store and retrieve data.

The Windows Registry is a hierarchical database consisting of Keys, Subkeys, Predefined Keys, Hives, and Value Entries, and is used to store both system-specific and application-specific data.

The Windows Registry stores information in a hierarchical structure, including user profiles, system hardware details, property settings, and data on installed programs.

Working with the Windows Registry in C#

You can programmatically interact with the Windows Registry in C# to read, write, and delete keys, subkeys, and values. Registry keys are similar to folders in the Windows system, with the ability to contain subkeys just as folders contain subfolders. To work with the Registry in C#, you can utilize the Registry class from the Microsoft.Win32 namespace.

Creating a New Subkey

You can create a new subkey with the CreateSubKey method.

Registry.CurrentUser.CreateSubKey(@"SOFTWARE\FoxLearn");

This creates a new subkey and returns a RegistryKey object. To add values to it, use the following code:

using (RegistryKey key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\FoxLearn"))
{
    key.SetValue("Key 1", "Value 1");
    key.SetValue("Key 2", "Value 2");
    key.Close();
}

Reading a Subkey Value

To read a value from a subkey, you can create a method like this:

static string ReadSubKeyValue(string subKey, string key)
{
    string str = string.Empty;
    using (RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(subKey))
    {
        if (registryKey != null)
        {
            str = registryKey.GetValue(key).ToString();
            registryKey.Close();
        }
    }
    return str;
}

To call this method:

static void Main(string[] args)
{
    string subKey = @"SOFTWARE\FoxLearn";
    string value = ReadSubKeyValue(subKey, "Key 1");
    Console.WriteLine(value);
    Console.Read();
}

Deleting a Subkey

To delete a subkey, use the DeleteSubKey method:

static bool DeleteKey(string KeyName)
{
    try
    {
        Registry.CurrentUser.DeleteSubKey(KeyName);
        return true;
    }
    catch
    {
        return false;
    }
}

This method returns true if the subkey was deleted successfully, and false otherwise.

Retrieving All Subkeys

To retrieve all the subkeys of a particular key, you can use the GetSubKeyNames method:

static List<string> GetChildSubKeys(string key)
{
    List<string> lstSubKeys = new List<string>();
    try
    {
        using (RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(key))
        {
            if (registryKey != null)
            {
                string[] subKeys = registryKey.GetSubKeyNames();
                lstSubKeys.AddRange(subKeys);
            }
        }
    }
    catch
    {
        // Handle exceptions here
    }
    return lstSubKeys;
}

To use this method and get all subkeys for a specific key:

List<string> subKeys = GetChildSubKeys(@"SOFTWARE\FoxLearn");

With these methods, you can easily manage keys, subkeys, and values in the Windows Registry in C#.