RegistryKey.CreateSubKey Method

Definition

Creates a new subkey or opens an existing subkey.

Overloads

CreateSubKey(String)

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

CreateSubKey(String, RegistryKeyPermissionCheck)

Creates a new subkey or opens an existing subkey for write access, using the specified permission check option.

CreateSubKey(String, Boolean)

Creates a new subkey or opens an existing subkey with the specified access. Available starting with .NET Framework 4.6.

CreateSubKey(String, RegistryKeyPermissionCheck, RegistrySecurity)

Creates a new subkey or opens an existing subkey for write access, using the specified permission check option and registry security.

CreateSubKey(String, Boolean, RegistryOptions)

Creates a new subkey or opens an existing subkey with the specified access. Available starting with .NET Framework 4.6.

CreateSubKey(String, RegistryKeyPermissionCheck, RegistryOptions)

Creates a subkey or opens a subkey for write access, using the specified permission check and registry options.

CreateSubKey(String, RegistryKeyPermissionCheck, RegistryOptions, RegistrySecurity)

Creates a subkey or opens a subkey for write access, using the specified permission check option, registry option, and registry security.

CreateSubKey(String)

Source:
RegistryKey.cs

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

C#
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey);

Parameters

subkey
String

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

Returns

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.

Exceptions

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.

-or-

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

Examples

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

C#
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 =
            Registry.CurrentUser.CreateSubKey("Test9999");
        // Create two subkeys under HKEY_CURRENT_USER\Test9999. The
        // keys are disposed when execution exits the using statement.
        using(RegistryKey
            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())
        {
            using(RegistryKey
                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,
                        tempKey.GetValue(valueName).ToString());
                }
            }
        }

        using(RegistryKey
            testSettings = test9999.OpenSubKey("TestSettings", true))
        {
            // Delete the ID value.
            testSettings.DeleteValue("id");

            // Verify the deletion.
            Console.WriteLine((string)testSettings.GetValue(
                "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')
        {
            Registry.CurrentUser.DeleteSubKeyTree("Test9999");
            Console.WriteLine("\nRegistry key {0} deleted.",
                test9999.Name);
        }
        else
        {
            Console.WriteLine("\nRegistry key {0} closed.",
                test9999.ToString());
            test9999.Close();
        }
    }
}

Remarks

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

Caution

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5

CreateSubKey(String, RegistryKeyPermissionCheck)

Source:
RegistryKey.cs

Creates a new subkey or opens an existing subkey for write access, using the specified permission check option.

C#
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck);

Parameters

subkey
String

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

permissionCheck
RegistryKeyPermissionCheck

One of the enumeration values that specifies whether the key is opened for read or read/write access.

Returns

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.

Attributes

Exceptions

subkey is null.

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

permissionCheck contains an invalid value.

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.

-or-

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

Remarks

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

Caution

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.

In order to use the OpenSubKey method, you must have an instance of the RegistryKey class. To get an instance of RegistryKey, use one of the static members of the Registry class.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5

CreateSubKey(String, Boolean)

Source:
RegistryKey.cs

Creates a new subkey or opens an existing subkey with the specified access. Available starting with .NET Framework 4.6.

C#
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, bool writable);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, bool writable);

Parameters

subkey
String

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

writable
Boolean

true to indicate the new subkey is writable; otherwise, false.

Returns

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.

Attributes

Exceptions

subkey is null.

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

The current 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.

-or-

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

Remarks

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

Caution

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.

In order to use the CreateSubKey method, you must have an instance of the RegistryKey class. To get an instance of RegistryKey, use one of the static members of the Registry class.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6, 7, 8, 9
.NET Framework 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5

CreateSubKey(String, RegistryKeyPermissionCheck, RegistrySecurity)

Source:
RegistryKey.cs

Creates a new subkey or opens an existing subkey for write access, using the specified permission check option and registry security.

C#
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, System.Security.AccessControl.RegistrySecurity? registrySecurity);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, System.Security.AccessControl.RegistrySecurity registrySecurity);
C#
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, System.Security.AccessControl.RegistrySecurity registrySecurity);

Parameters

subkey
String

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

permissionCheck
RegistryKeyPermissionCheck

One of the enumeration values that specifies whether the key is opened for read or read/write access.

registrySecurity
RegistrySecurity

The access control security for the new key.

Returns

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.

Attributes

Exceptions

subkey is null.

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

permissionCheck contains an invalid value.

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

The current 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.

-or-

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

Remarks

The CreateSubKey method creates a registry key that has the access control specified by the registrySecurity parameter. The RegistryKey object that is returned represents the registry key, but that object is not restricted by the access control specified in the registrySecurity parameter.

If permissionCheck is RegistryKeyPermissionCheck.ReadWriteSubTree, the key is opened for read/write access. If permissionCheck is RegistryKeyPermissionCheck.ReadSubTree, the key is opened for read access.

For backward compatibility, the key is opened for reading and writing if permissionCheck is RegistryKeyPermissionCheck.Default and the parent key also has RegistryKeyPermissionCheck.Default. If the parent key has any other setting, read/write status is controlled by the parent key's setting.

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

Caution

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.

In order to use the OpenSubKey method, you must have an instance of the RegistryKey class. To get an instance of RegistryKey, use one of the static members of the Registry class.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5

CreateSubKey(String, Boolean, RegistryOptions)

Source:
RegistryKey.cs

Creates a new subkey or opens an existing subkey with the specified access. Available starting with .NET Framework 4.6.

C#
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, bool writable, Microsoft.Win32.RegistryOptions options);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, bool writable, Microsoft.Win32.RegistryOptions options);

Parameters

subkey
String

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

writable
Boolean

true to indicate the new subkey is writable; otherwise, false.

options
RegistryOptions

The registry option to use.

Returns

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.

Attributes

Exceptions

subkey is null.

options does not specify a valid Option.

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

The current 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.

-or-

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

Remarks

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

Caution

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.

In order to use the CreateSubKey method, you must have an instance of the RegistryKey class. To get an instance of RegistryKey, use one of the static members of the Registry class.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6, 7, 8, 9
.NET Framework 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5

CreateSubKey(String, RegistryKeyPermissionCheck, RegistryOptions)

Source:
RegistryKey.cs

Creates a subkey or opens a subkey for write access, using the specified permission check and registry options.

C#
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, Microsoft.Win32.RegistryOptions registryOptions);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, Microsoft.Win32.RegistryOptions options);

Parameters

subkey
String

The name or path of the subkey to create or open.

permissionCheck
RegistryKeyPermissionCheck

One of the enumeration values that specifies whether the key is opened for read or read/write access.

registryOptionsoptions
RegistryOptions

The registry option to use; for example, that creates a volatile key.

Returns

The newly created subkey, or null if the operation failed.

Attributes

Exceptions

subkey is null.

The current RegistryKey object is closed (closed keys cannot be accessed).

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

The nesting level exceeds 510.

-or-

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

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

Remarks

To obtain the current RegistryKey object, specify an empty string ("") for subkey.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5

CreateSubKey(String, RegistryKeyPermissionCheck, RegistryOptions, RegistrySecurity)

Source:
RegistryKey.cs

Creates a subkey or opens a subkey for write access, using the specified permission check option, registry option, and registry security.

C#
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, Microsoft.Win32.RegistryOptions registryOptions, System.Security.AccessControl.RegistrySecurity? registrySecurity);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, Microsoft.Win32.RegistryOptions registryOptions, System.Security.AccessControl.RegistrySecurity registrySecurity);
C#
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, Microsoft.Win32.RegistryOptions registryOptions, System.Security.AccessControl.RegistrySecurity registrySecurity);

Parameters

subkey
String

The name or path of the subkey to create or open.

permissionCheck
RegistryKeyPermissionCheck

One of the enumeration values that specifies whether the key is opened for read or read/write access.

registryOptions
RegistryOptions

The registry option to use.

registrySecurity
RegistrySecurity

The access control security for the new subkey.

Returns

The newly created subkey, or null if the operation failed.

Attributes

Exceptions

subkey is null.

The current RegistryKey object is closed. Closed keys cannot be accessed.

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

The nesting level exceeds 510.

-or-

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

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

Remarks

To obtain the current RegistryKey object, specify an empty string ("") for subkey.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5