RegistryKey.CreateSubKey Method (String, RegistryKeyPermissionCheck, RegistrySecurity)


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

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

public RegistryKey CreateSubKey(
	string subkey,
	RegistryKeyPermissionCheck permissionCheck,
	RegistrySecurity registrySecurity


Type: System.String

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

Type: Microsoft.Win32.RegistryKeyPermissionCheck

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

Type: System.Security.AccessControl.RegistrySecurity

The access control security for the new key.

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.

Exception Condition

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.


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

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.


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.


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: RegistryPermissionAccess.Write, RegistryPermissionAccess.Create


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

.NET Framework
Available since 2.0
Return to top