This documentation is archived and is not being maintained.

RegistryKey.OpenSubKey Method (String, RegistryKeyPermissionCheck)

Retrieves the specified subkey for read or read/write access.

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

public RegistryKey OpenSubKey(
	string name,
	RegistryKeyPermissionCheck permissionCheck


Type: System.String
The name or path of the subkey to create or open.
Type: Microsoft.Win32.RegistryKeyPermissionCheck
One of the enumeration values that specifies whether the key is opened for read or read/write access.

Return Value

Type: Microsoft.Win32.RegistryKey
The subkey requested, or null if the operation failed.


name is null


permissionCheck contains an invalid value.


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


The user does not have the permissions required to read the registry key.

Rather than throwing an exception, this method returns null if the requested key does not exist.

If permissionCheck is RegistryKeyPermissionCheck.ReadWriteSubTree, the key is opened for reading and writing; if permissionCheck is RegistryKeyPermissionCheck.ReadSubTree or RegistryKeyPermissionCheck.Default, the key is opened for reading unless the parent key was opened with RegistryKeyPermissionCheck.ReadWriteSubTree.

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.

The following code example creates a subkey containing 100 key/value pairs and closes it. The example opens the subkey with Default, records the time it takes to read all the values, and closes the subkey. The example opens the subkey with ReadSubTree and records the time it takes to read all the values. Finally, the example computes and displays the percentage improvement.

using System;
using Microsoft.Win32;
using System.Diagnostics;

public class Example
    public static void Main()
        const int LIMIT = 100;
        RegistryKey cu = Registry.CurrentUser;
        const string testKey = "RegistryKeyPermissionCheckExample";

        Console.WriteLine("Generating {0} key/value pairs.", LIMIT);
        RegistryKey rk = cu.CreateSubKey(testKey);
        for (int i = 0; i < LIMIT; i++)
            rk.SetValue("Key" + i, i);


        Stopwatch s = new Stopwatch();

        // On the default setting, security is checked every time
        // a key/value pair is read.
        rk = cu.OpenSubKey(testKey, RegistryKeyPermissionCheck.Default);

        for (int i = 0; i < LIMIT; i++)
            rk.GetValue("Key" + i, i);
        long delta1 = s.ElapsedTicks;


        // When the key is opened with ReadSubTree, security is 
        // not checked when the values are read.
        rk = cu.OpenSubKey(testKey, RegistryKeyPermissionCheck.ReadSubTree);

        for (int i = 0; i < LIMIT; i++)
            rk.GetValue("Key" + i, i);
        long delta2 = s.ElapsedTicks;

        double faster = (double) (delta1 - delta2) / (double) delta1;
        Console.WriteLine("ReadSubTree is {0}% faster for {1} values.",
            (faster * 100).ToString("0.0"), LIMIT);


/* This code example produces output similar to the following:

Generating 100 key/value pairs.
ReadSubTree is 23.4% faster for 100 values.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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