RegistryKey.CreateSubKey RegistryKey.CreateSubKey RegistryKey.CreateSubKey RegistryKey.CreateSubKey Method

Definition

Creates a new subkey or opens an existing subkey.

Overloads

CreateSubKey(String) CreateSubKey(String) CreateSubKey(String) CreateSubKey(String)

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

CreateSubKey(String, RegistryKeyPermissionCheck) CreateSubKey(String, RegistryKeyPermissionCheck) CreateSubKey(String, RegistryKeyPermissionCheck) CreateSubKey(String, RegistryKeyPermissionCheck)

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

CreateSubKey(String, Boolean) CreateSubKey(String, Boolean) CreateSubKey(String, Boolean) CreateSubKey(String, Boolean)

Creates a new subkey or opens an existing subkey with the specified access.

Available starting in .NET Framework 4.6

CreateSubKey(String, RegistryKeyPermissionCheck, RegistrySecurity) CreateSubKey(String, RegistryKeyPermissionCheck, RegistrySecurity) 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) CreateSubKey(String, Boolean, RegistryOptions) CreateSubKey(String, Boolean, RegistryOptions) CreateSubKey(String, Boolean, RegistryOptions)

Creates a new subkey or opens an existing subkey with the specified access.

Available starting in .NET Framework 4.6

CreateSubKey(String, RegistryKeyPermissionCheck, RegistryOptions) CreateSubKey(String, RegistryKeyPermissionCheck, RegistryOptions) CreateSubKey(String, RegistryKeyPermissionCheck, RegistryOptions) 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) CreateSubKey(String, RegistryKeyPermissionCheck, RegistryOptions, RegistrySecurity) 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) CreateSubKey(String) CreateSubKey(String) CreateSubKey(String)

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

public:
 Microsoft::Win32::RegistryKey ^ CreateSubKey(System::String ^ subkey);
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey);
member this.CreateSubKey : string -> Microsoft.Win32.RegistryKey
Public Function CreateSubKey (subkey As String) As RegistryKey
Parameters
subkey
String String String 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

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.

using namespace System;
using namespace System::Security::Permissions;
using namespace Microsoft::Win32;

int main()
{
   // Create a subkey named Test9999 under HKEY_CURRENT_USER.
   RegistryKey ^ test9999 = Registry::CurrentUser->CreateSubKey( "Test9999" );

   // Create two subkeys under HKEY_CURRENT_USER\Test9999.
   test9999->CreateSubKey( "TestName" )->Close();
   RegistryKey ^ testSettings = test9999->CreateSubKey( "TestSettings" );

   // Create data for the TestSettings subkey.
   testSettings->SetValue( "Language", "French" );
   testSettings->SetValue( "Level", "Intermediate" );
   testSettings->SetValue( "ID", 123 );
   testSettings->Close();

   // Print the information from the Test9999 subkey.
   Console::WriteLine( "There are {0} subkeys under Test9999.", test9999->SubKeyCount.ToString() );
   array<String^>^subKeyNames = test9999->GetSubKeyNames();
   for ( int i = 0; i < subKeyNames->Length; i++ )
   {
      RegistryKey ^ tempKey = test9999->OpenSubKey( subKeyNames[ i ] );
      Console::WriteLine( "\nThere are {0} values for {1}.", tempKey->ValueCount.ToString(), tempKey->Name );
      array<String^>^valueNames = tempKey->GetValueNames();
      for ( int j = 0; j < valueNames->Length; j++ )
      {
         Console::WriteLine( "{0,-8}: {1}", valueNames[ j ], tempKey->GetValue( valueNames[ j ] )->ToString() );

      }
   }
   
   // Delete the ID value.
   testSettings = test9999->OpenSubKey( "TestSettings", true );
   testSettings->DeleteValue( "id" );

   // Verify the deletion.
   Console::WriteLine( dynamic_cast<String^>(testSettings->GetValue(  "id", "ID not found." )) );
   testSettings->Close();

   // 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();
   }
}
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();
        }
    }
}
Imports Microsoft.VisualBasic
Imports System
Imports System.Security.Permissions
Imports Microsoft.Win32

Public Class RegKey
    Shared Sub Main()

        ' Create a subkey named Test9999 under HKEY_CURRENT_USER.
        Dim test9999 As RegistryKey = _
            Registry.CurrentUser.CreateSubKey("Test9999")

        ' Create two subkeys under HKEY_CURRENT_USER\Test9999.
        test9999.CreateSubKey("TestName").Close()
        Dim testSettings As RegistryKey = _
            test9999.CreateSubKey("TestSettings")

        ' Create data for the TestSettings subkey.
        testSettings.SetValue("Language", "French")
        testSettings.SetValue("Level", "Intermediate")
        testSettings.SetValue("ID", 123)
        testSettings.Close()

        ' Print the information from the Test9999 subkey.
        Console.WriteLine("There are {0} subkeys under Test9999.", _
            test9999.SubKeyCount.ToString())
        For Each subKeyName As String In test9999.GetSubKeyNames()
            Dim tempKey As RegistryKey = _
                test9999.OpenSubKey(subKeyName)
            Console.WriteLine(vbCrLf & "There are {0} values for " & _
                "{1}.", tempKey.ValueCount.ToString(), tempKey.Name)
            For Each valueName As String In tempKey.GetValueNames()
                Console.WriteLine("{0,-8}: {1}", valueName, _
                    tempKey.GetValue(valueName).ToString())
            Next
        Next

        ' Delete the ID value.
        testSettings = test9999.OpenSubKey("TestSettings", True)
        testSettings.DeleteValue("id")

        ' Verify the deletion.
        Console.WriteLine(CType(testSettings.GetValue( _
            "id", "ID not found."), String))
        testSettings.Close()

        ' Delete or close the new subkey.
        Console.Write(vbCrLf & "Delete newly created " & _
            "registry key? (Y/N) ")
        If Char.ToUpper(Convert.ToChar(Console.Read())) = "Y"C Then
            Registry.CurrentUser.DeleteSubKeyTree("Test9999")
            Console.WriteLine(vbCrLf & "Registry key {0} deleted.", _
                test9999.Name)
        Else
            Console.WriteLine(vbCrLf & "Registry key {0} closed.", _
                test9999.ToString())
            test9999.Close()
        End If
   
    End Sub
End Class

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.

Security

RegistryPermission
for the ability to modify the specified registry key if it exists, or for the ability to create the registry key if it does not already exist. Associated enumerations: Write, Create

SecurityPermission
for the ability to access the specified registry key if it is a remote key. Associated enumeration: UnmanagedCode

See Also

CreateSubKey(String, RegistryKeyPermissionCheck) CreateSubKey(String, RegistryKeyPermissionCheck) CreateSubKey(String, RegistryKeyPermissionCheck) CreateSubKey(String, RegistryKeyPermissionCheck)

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

public:
 Microsoft::Win32::RegistryKey ^ CreateSubKey(System::String ^ subkey, Microsoft::Win32::RegistryKeyPermissionCheck permissionCheck);
[System.Runtime.InteropServices.ComVisible(false)]
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck);
member this.CreateSubKey : string * Microsoft.Win32.RegistryKeyPermissionCheck -> Microsoft.Win32.RegistryKey
Public Function CreateSubKey (subkey As String, permissionCheck As RegistryKeyPermissionCheck) As RegistryKey
Parameters
subkey
String String String String

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

permissionCheck
RegistryKeyPermissionCheck RegistryKeyPermissionCheck RegistryKeyPermissionCheck 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.

Exceptions

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.

Security

RegistryPermission
for the ability to modify the specified registry key if it exists, or for the ability to create the registry key if it does not already exist. Associated enumerations: Write, Create

SecurityPermission
for the ability to access the specified registry key if it is a remote key. Associated enumeration: UnmanagedCode

See Also

CreateSubKey(String, Boolean) CreateSubKey(String, Boolean) CreateSubKey(String, Boolean) CreateSubKey(String, Boolean)

Creates a new subkey or opens an existing subkey with the specified access.

Available starting in .NET Framework 4.6

public:
 Microsoft::Win32::RegistryKey ^ CreateSubKey(System::String ^ subkey, bool writable);
[System.Runtime.InteropServices.ComVisible(false)]
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, bool writable);
member this.CreateSubKey : string * bool -> Microsoft.Win32.RegistryKey
Public Function CreateSubKey (subkey As String, writable As Boolean) As RegistryKey
Parameters
subkey
String String String String

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

writable
Boolean Boolean Boolean 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.

Exceptions

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.

CreateSubKey(String, RegistryKeyPermissionCheck, RegistrySecurity) CreateSubKey(String, RegistryKeyPermissionCheck, RegistrySecurity) 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.

public:
 Microsoft::Win32::RegistryKey ^ CreateSubKey(System::String ^ subkey, Microsoft::Win32::RegistryKeyPermissionCheck permissionCheck, System::Security::AccessControl::RegistrySecurity ^ registrySecurity);
[System.Runtime.InteropServices.ComVisible(false)]
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, System.Security.AccessControl.RegistrySecurity registrySecurity);
member this.CreateSubKey : string * Microsoft.Win32.RegistryKeyPermissionCheck * System.Security.AccessControl.RegistrySecurity -> Microsoft.Win32.RegistryKey
Parameters
subkey
String String String String

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

permissionCheck
RegistryKeyPermissionCheck RegistryKeyPermissionCheck RegistryKeyPermissionCheck RegistryKeyPermissionCheck

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

registrySecurity
RegistrySecurity RegistrySecurity 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.

Exceptions

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.

Security

RegistryPermission
for the ability to modify the specified registry key if it exists, or for the ability to create the registry key if it does not already exist. Associated enumerations: Write, Create

SecurityPermission
for the ability to access the specified registry key if it is a remote key. Associated enumeration: UnmanagedCode

See Also

CreateSubKey(String, Boolean, RegistryOptions) CreateSubKey(String, Boolean, RegistryOptions) CreateSubKey(String, Boolean, RegistryOptions) CreateSubKey(String, Boolean, RegistryOptions)

Creates a new subkey or opens an existing subkey with the specified access.

Available starting in .NET Framework 4.6

public:
 Microsoft::Win32::RegistryKey ^ CreateSubKey(System::String ^ subkey, bool writable, Microsoft::Win32::RegistryOptions options);
[System.Runtime.InteropServices.ComVisible(false)]
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, bool writable, Microsoft.Win32.RegistryOptions options);
member this.CreateSubKey : string * bool * Microsoft.Win32.RegistryOptions -> Microsoft.Win32.RegistryKey
Public Function CreateSubKey (subkey As String, writable As Boolean, options As RegistryOptions) As RegistryKey
Parameters
subkey
String String String String

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

writable
Boolean Boolean Boolean Boolean

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

options
RegistryOptions RegistryOptions RegistryOptions 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.

Exceptions

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.

CreateSubKey(String, RegistryKeyPermissionCheck, RegistryOptions) CreateSubKey(String, RegistryKeyPermissionCheck, RegistryOptions) CreateSubKey(String, RegistryKeyPermissionCheck, RegistryOptions) CreateSubKey(String, RegistryKeyPermissionCheck, RegistryOptions)

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

public:
 Microsoft::Win32::RegistryKey ^ CreateSubKey(System::String ^ subkey, Microsoft::Win32::RegistryKeyPermissionCheck permissionCheck, Microsoft::Win32::RegistryOptions options);
[System.Runtime.InteropServices.ComVisible(false)]
public Microsoft.Win32.RegistryKey CreateSubKey (string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, Microsoft.Win32.RegistryOptions registryOptions);
member this.CreateSubKey : string * Microsoft.Win32.RegistryKeyPermissionCheck * Microsoft.Win32.RegistryOptions -> Microsoft.Win32.RegistryKey
Public Function CreateSubKey (subkey As String, permissionCheck As RegistryKeyPermissionCheck, options As RegistryOptions) As RegistryKey
Parameters
subkey
String String String String

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

permissionCheck
RegistryKeyPermissionCheck RegistryKeyPermissionCheck RegistryKeyPermissionCheck RegistryKeyPermissionCheck

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

options
RegistryOptions RegistryOptions RegistryOptions RegistryOptions

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

Returns

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

Exceptions

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.

CreateSubKey(String, RegistryKeyPermissionCheck, RegistryOptions, RegistrySecurity) CreateSubKey(String, RegistryKeyPermissionCheck, RegistryOptions, RegistrySecurity) 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.

public:
 Microsoft::Win32::RegistryKey ^ CreateSubKey(System::String ^ subkey, Microsoft::Win32::RegistryKeyPermissionCheck permissionCheck, Microsoft::Win32::RegistryOptions registryOptions, System::Security::AccessControl::RegistrySecurity ^ registrySecurity);
[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);
member this.CreateSubKey : string * Microsoft.Win32.RegistryKeyPermissionCheck * Microsoft.Win32.RegistryOptions * System.Security.AccessControl.RegistrySecurity -> Microsoft.Win32.RegistryKey
Parameters
subkey
String String String String

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

permissionCheck
RegistryKeyPermissionCheck RegistryKeyPermissionCheck RegistryKeyPermissionCheck RegistryKeyPermissionCheck

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

registryOptions
RegistryOptions RegistryOptions RegistryOptions RegistryOptions

The registry option to use.

registrySecurity
RegistrySecurity RegistrySecurity RegistrySecurity RegistrySecurity

The access control security for the new subkey.

Returns

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

Exceptions

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