Namespace: Microsoft.Win32
Assembly: System.Runtime.InteropServices (in System.Runtime.InteropServices.dll)

RegistryKey Class

Represents a key in the Windows registry. This class cannot be inherited.

Summary

The RegistryKey class provides access to the Windows registry. You can use this class to read and write registry values, create subkeys, and manage registry data. It represents a node within the registry hierarchy.

Syntax

public sealed class RegistryKey

Inheritance

System.Object
System.MarshalByRefObject
Microsoft.Win32.RegistryKey

Remarks

The Windows 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. The RegistryKey class provides a managed wrapper around the registry's native functionality.

Access to the registry is typically restricted. You may need appropriate permissions to perform certain operations, such as writing to critical registry locations. The RegistryKey class interacts with the registry's standard hives like HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, and HKEY_CURRENT_CONFIG.

When you are finished with a RegistryKey object, you should call its Dispose method to release any unmanaged resources. Using a using statement is the recommended way to ensure that Dispose is called.

Members

Properties

Handle
Gets the native handle to the registry key.
Name
Gets the full path to the registry key.
SubKeyCount
Gets the number of subkeys under the current key.
ValueCount
Gets the number of values under the current key.

Methods

Close()
Closes the registry key. This is equivalent to calling Dispose().
CreateSubKey(string subkey)
Creates a subkey under the current key, or opens it if it already exists.
CreateSubKey(string subkey, bool writable)
Creates a subkey under the current key, or opens it if it already exists, and specifies whether the key is writable.
DeleteSubKey(string subkey)
Deletes a subkey under the current key.
DeleteSubKey(string subkey, bool throwOnSubKeyNotFound)
Deletes a subkey under the current key, and optionally throws an exception if the subkey is not found.
DeleteValue(string name)
Deletes the specified value from the registry key.
Dispose()
Releases all resources used by the current instance of the RegistryKey class.
Flush()
Writes all the buffered information to the registry.
GetValue(string name)
Retrieves the data associated with the specified name in the registry key.
GetValue(string name, object defaultValue)
Retrieves the data associated with the specified name in the registry key, or the specified default value if the value is not found.
GetValueKind(string name)
Gets the registry value kind of the specified value.
OpenRemoteBaseKey(RegistryHive hive, string machineName)
Opens the specified registry hive on a remote computer.
OpenSubKey(string name)
Opens a subkey under the current key. This subkey is read-only.
OpenSubKey(string name, bool writable)
Opens a subkey under the current key, specifying whether the subkey is writable.
SetValue(string name, object value)
Creates or updates a registry key-value pair.
SetValue(string name, object value, RegistryValueKind valueKind)
Creates or updates a registry key-value pair with the specified registry value kind.

Static Properties

ClassesRoot
Gets a key to the HKEY_CLASSES_ROOT logical registry root.
CurrentConfig
Gets a key to the HKEY_CURRENT_CONFIG logical registry root.
CurrentUser
Gets a key to the HKEY_CURRENT_USER logical registry root.
DynData
Gets a key to the HKEY_DYN_DATA logical registry root. This hive is not available on all platforms.
LocalMachine
Gets a key to the HKEY_LOCAL_MACHINE logical registry root.
PerformanceData
Gets a key to the HKEY_PERFORMANCE_DATA logical registry root. This hive is not available on all platforms.
Users
Gets a key to the HKEY_USERS logical registry root.

Example

The following example demonstrates how to create a new registry key, write a string value to it, and then read the value back. The key is created under HKEY_CURRENT_USER\Software\MyApplication.


using Microsoft.Win32;
using System;

public class RegistryExample
{
    public static void Main(string[] args)
    {
        string subKeyPath = "Software\\MyApplication";
        string valueName = "Setting1";
        string valueData = "Hello Registry!";

        try
        {
            // Open or create the registry key under HKEY_CURRENT_USER
            using (RegistryKey key = Registry.CurrentUser.CreateSubKey(subKeyPath))
            {
                if (key != null)
                {
                    // Write a string value to the key
                    key.SetValue(valueName, valueData, RegistryValueKind.String);
                    Console.WriteLine($"Value '{valueName}' set to '{valueData}' in {key.Name}");

                    // Read the value back
                    object readValue = key.GetValue(valueName);
                    if (readValue != null)
                    {
                        Console.WriteLine($"Read value '{valueName}': {readValue}");
                    }
                    else
                    {
                        Console.WriteLine($"Value '{valueName}' not found.");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }

        // Example of deleting a subkey (use with caution)
        // try
        // {
        //     Registry.CurrentUser.DeleteSubKeyTree(subKeyPath);
        //     Console.WriteLine($"Subkey '{subKeyPath}' deleted.");
        // }
        // catch (Exception ex)
        // {
        //     Console.WriteLine($"Error deleting subkey: {ex.Message}");
        // }
    }
}