RegistryKey Class

Namespace: Microsoft.Win32

Assembly: System.Runtime.InteropServices

Represents a key in the Windows Registry.

Description

The RegistryKey class provides the fundamental interface for interacting with the Windows Registry. It allows you to navigate through the registry hierarchy, create, delete, and modify keys and values, and retrieve information about them. The registry is a hierarchical database that stores low-level settings for the Microsoft Windows operating system and for applications that opt to use the registry to store configuration settings.

Supported Platforms

The RegistryKey class is available on Windows operating systems. Accessing the registry on other platforms may lead to unexpected behavior or errors.

Syntax

public sealed class RegistryKey

Inheritance

Object
RegistryKey

Remarks

The Windows Registry is divided into several root keys, such as HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, and HKEY_USERS. The RegistryKey class provides static properties to access these root keys (e.g., Registry.CurrentUser).

When working with the registry, it's crucial to handle potential exceptions such as UnauthorizedAccessException if you do not have the necessary permissions to access a particular key or value.

Properties

Name Description
Handle Gets a handle to the registry key. This is an opaque handle and should not be directly manipulated.
Name Gets the full name of the registry key.
SubKeyCount Gets the number of subkeys contained in the registry key.
ValueCount Gets the number of values associated with the registry key.

Methods

CreateSubKey

Creates a new subkey or opens an existing subkey under the current key.

public RegistryKey CreateSubKey(string subkey)

Parameters:

Returns: The RegistryKey representing the new or existing subkey.

DeleteSubKey

Deletes a subkey and its contents from the registry.

public void DeleteSubKey(string subkey)

Parameters:

DeleteValue

Deletes a value from the registry key.

public void DeleteValue(string name)

Parameters:

Flush

Writes all changes for the current registry key and any child keys to the registry.

public void Flush()

GetValue

Retrieves the data associated with the specified name in the registry key.

public object GetValue(string name)

Parameters:

Returns: The data associated with the specified name, or null if the value is not found.

SetValue

Creates or updates a value in the registry key.

public void SetValue(string name, object value)

Parameters:

Examples

Creating and Writing to a Registry Value

using Microsoft.Win32;

try
{
    // Access the CurrentUser hive and create a subkey if it doesn't exist
    using (RegistryKey key = Registry.CurrentUser.CreateSubKey("MyApplicationSettings"))
    {
        if (key != null)
        {
            // Set a string value
            key.SetValue("UserName", "JohnDoe");

            // Set a DWORD (32-bit integer) value
            key.SetValue("MaxConnections", 100, RegistryValueKind.DWord);

            Console.WriteLine("Registry values set successfully.");
        }
    }
}
catch (UnauthorizedAccessException ex)
{
    Console.WriteLine($"Access denied: {ex.Message}");
}
catch (Exception ex)
{
    Console.WriteLine($"An error occurred: {ex.Message}");
}

Reading from the Registry

using Microsoft.Win32;

try
{
    // Access the CurrentUser hive and the previously created subkey
    using (RegistryKey key = Registry.CurrentUser.OpenSubKey("MyApplicationSettings"))
    {
        if (key != null)
        {
            // Get a string value
            object userName = key.GetValue("UserName");
            if (userName != null)
            {
                Console.WriteLine($"UserName: {userName}");
            }
            else
            {
                Console.WriteLine("UserName value not found.");
            }

            // Get a DWORD value
            object maxConnections = key.GetValue("MaxConnections");
            if (maxConnections != null)
            {
                Console.WriteLine($"MaxConnections: {maxConnections}");
            }
            else
            {
                Console.WriteLine("MaxConnections value not found.");
            }
        }
        else
        {
            Console.WriteLine("MyApplicationSettings key not found.");
        }
    }
}
catch (UnauthorizedAccessException ex)
{
    Console.WriteLine($"Access denied: {ex.Message}");
}
catch (Exception ex)
{
    Console.WriteLine($"An error occurred: {ex.Message}");
}

Deleting a Subkey

using Microsoft.Win32;

try
{
    // Access the CurrentUser hive and delete the subkey
    Registry.CurrentUser.DeleteSubKey("MyApplicationSettings", false); // The 'false' parameter prevents an exception if the key doesn't exist
    Console.WriteLine("MyApplicationSettings subkey deleted (if it existed).");
}
catch (UnauthorizedAccessException ex)
{
    Console.WriteLine($"Access denied: {ex.Message}");
}
catch (Exception ex)
{
    Console.WriteLine($"An error occurred: {ex.Message}");
}

See Also