Microsoft Docs

Windows API

System.Registry

The System.Registry namespace provides classes that allow you to access and manipulate the Windows registry. This namespace is part of the .NET Framework and can be used from C#, VB.NET, and any other .NET language.

Key Classes

  • Registry – Provides static methods to retrieve predefined registry keys.
  • RegistryKey – Represents a key-level node in the registry hierarchy and provides methods for creating, opening, and deleting subkeys and values.

Typical Usage

Below is a simple example that reads a string value from HKEY_CURRENT_USER\Software\MyApp.


// Read a value from the registry
using Microsoft.Win32;

string subKey = @"Software\MyApp";
string valueName = "InstallPath";

using (RegistryKey key = Registry.CurrentUser.OpenSubKey(subKey))
{
    if (key != null)
    {
        object val = key.GetValue(valueName);
        if (val != null)
        {
            Console.WriteLine($"Install path: {val}");
        }
        else
        {
            Console.WriteLine("Value not found.");
        }
    }
}

Creating a Subkey


// Create a new subkey and set a value
using Microsoft.Win32;

string subKey = @"Software\MyApp\Settings";
using (RegistryKey key = Registry.CurrentUser.CreateSubKey(subKey))
{
    key.SetValue("Theme", "Dark", RegistryValueKind.String);
}

Deleting a Subkey


// Delete a subkey recursively
using Microsoft.Win32;

string subKey = @"Software\MyApp\Obsolete";
Registry.CurrentUser.DeleteSubKeyTree(subKey, false);

Related Topics