Export (0) Print
Expand All

RegistryKey.CreateSubKey Method (String)

Creates a new subkey or opens an existing subkey for write access.

Namespace:  Microsoft.Win32
Assembly:  mscorlib (in mscorlib.dll)

public RegistryKey CreateSubKey(
	string subkey


Type: System.String

The name or path of the subkey to create or open. This string is not case-sensitive.

Return Value

Type: Microsoft.Win32.RegistryKey
The newly created subkey, or null if the operation failed. If a zero-length string is specified for subkey, the current RegistryKey object is returned.


subkey is null.


The user does not have the permissions required to create or open the registry key.


The RegistryKey on which this method is being invoked is closed (closed keys cannot be accessed).


The RegistryKey cannot be written to; for example, it was not opened as a writable key , or the user does not have the necessary access rights.


The nesting level exceeds 510.


A system error occurred, such as deletion of the key, or an attempt to create a key in the LocalMachine root.

In order to perform this action, the user must have permission at this level and below in the registry hierarchy.

Caution noteCaution

Do not expose RegistryKey objects in such a way that a malicious program could create thousands of meaningless subkeys or key/value pairs. For example, do not allow callers to enter arbitrary keys or values.

The following code example shows how to create a subkey under HKEY_CURRENT_USER, manipulate its contents, and then delete the subkey.

using System;
using System.Security.Permissions;
using Microsoft.Win32;

class RegKey
    static void Main()
        // Create a subkey named Test9999 under HKEY_CURRENT_USER.
        RegistryKey test9999 = 
        // Create two subkeys under HKEY_CURRENT_USER\Test9999. The 
        // keys are disposed when execution exits the using statement. 
            testName = test9999.CreateSubKey("TestName"),
            testSettings = test9999.CreateSubKey("TestSettings"))
            // Create data for the TestSettings subkey.
            testSettings.SetValue("Language", "French");
            testSettings.SetValue("Level", "Intermediate");
            testSettings.SetValue("ID", 123);

        // Print the information from the Test9999 subkey.
        Console.WriteLine("There are {0} subkeys under {1}.", 
            test9999.SubKeyCount.ToString(), test9999.Name);
        foreach(string subKeyName in test9999.GetSubKeyNames())
                tempKey = test9999.OpenSubKey(subKeyName))
                Console.WriteLine("\nThere are {0} values for {1}.", 
                    tempKey.ValueCount.ToString(), tempKey.Name);
                foreach(string valueName in tempKey.GetValueNames())
                    Console.WriteLine("{0,-8}: {1}", valueName, 

            testSettings = test9999.OpenSubKey("TestSettings", true))
            // Delete the ID value.

            // Verify the deletion.
                "id", "ID not found."));

        // Delete or close the new subkey.
        Console.Write("\nDelete newly created registry key? (Y/N) ");
        if(Char.ToUpper(Convert.ToChar(Console.Read())) == 'Y')
            Console.WriteLine("\nRegistry key {0} deleted.", 
            Console.WriteLine("\nRegistry key {0} closed.", 

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft