RegistryKey.OpenSubKey Method (String, Boolean) (Microsoft.Win32)

Switch View :
ScriptFree
.NET Framework Class Library
RegistryKey.OpenSubKey Method (String, Boolean)

Retrieves a specified subkey, and specifies whether write access is to be applied to the key.

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

Visual Basic
Public Function OpenSubKey ( _
	name As String, _
	writable As Boolean _
) As RegistryKey
C#
public RegistryKey OpenSubKey(
	string name,
	bool writable
)
Visual C++
public:
RegistryKey^ OpenSubKey(
	String^ name, 
	bool writable
)
F#
member OpenSubKey : 
        name:string * 
        writable:bool -> RegistryKey 

Parameters

name
Type: System.String
Name or path of the subkey to open.
writable
Type: System.Boolean
Set to true if you need write access to the key.

Return Value

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

Exception Condition
ArgumentNullException

name is null.

ObjectDisposedException

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

SecurityException

The user does not have the permissions required to access the registry key in the specified mode.

Remarks

If the requested key does not exist, this method returns null instead of throwing an exception.

If writable is true, the key will be opened for reading and writing, otherwise, the key will be opened as read-only.

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

Examples

The following code example creates a test key and uses the OpenSubKey method to open it, demonstrating both overloads of the method.

Visual Basic

Imports System
Imports Microsoft.Win32
Imports Microsoft.VisualBasic

Public Class Example
    Public Shared Sub Main()
        ' Delete and recreate the test key.
        Registry.CurrentUser.DeleteSubKey("RegistryOpenSubKeyExample", False)
        Dim rk As RegistryKey = Registry.CurrentUser.CreateSubKey("RegistryOpenSubKeyExample")
        rk.Close

        ' Obtain an instance of RegistryKey for the CurrentUser registry 
        ' root. 
        Dim rkCurrentUser As RegistryKey = Registry.CurrentUser

        ' Obtain the test key (read-only) and display it.
        Dim rkTest As RegistryKey = rkCurrentUser.OpenSubKey("RegistryOpenSubKeyExample")
        Console.WriteLine("Test key: {0}", rkTest)
        rkTest.Close
        rkCurrentUser.Close

        ' Obtain the test key in one step, using the CurrentUser registry 
        ' root.
        rkTest = Registry.CurrentUser.OpenSubKey("RegistryOpenSubKeyExample")
        Console.WriteLine("Test key: {0}", rkTest)
        rkTest.Close

        ' Obtain the test key in read/write mode.
        rkTest = Registry.CurrentUser.OpenSubKey("RegistryOpenSubKeyExample", True)
        rkTest.SetValue("TestName", "TestValue")
        Console.WriteLine("Test value for TestName: {0}", rkTest.GetValue("TestName"))
        rkTest.Close
    End Sub 'Main
End Class 'Example


C#

using System;
using Microsoft.Win32;
using Microsoft.VisualBasic;

public class Example
{
    public static void Main()
    {
        // Delete and recreate the test key.
        Registry.CurrentUser.DeleteSubKey("RegistryOpenSubKeyExample", false);
        RegistryKey rk = Registry.CurrentUser.CreateSubKey("RegistryOpenSubKeyExample");
        rk.Close();

        // Obtain an instance of RegistryKey for the CurrentUser registry 
        // root. 
        RegistryKey rkCurrentUser = Registry.CurrentUser;

        // Obtain the test key (read-only) and display it.
        RegistryKey rkTest = rkCurrentUser.OpenSubKey("RegistryOpenSubKeyExample");
        Console.WriteLine("Test key: {0}", rkTest);
        rkTest.Close();
        rkCurrentUser.Close();

        // Obtain the test key in one step, using the CurrentUser registry 
        // root.
        rkTest = Registry.CurrentUser.OpenSubKey("RegistryOpenSubKeyExample");
        Console.WriteLine("Test key: {0}", rkTest);
        rkTest.Close();

        // Open the test key in read/write mode.
        rkTest = Registry.CurrentUser.OpenSubKey("RegistryOpenSubKeyExample", true);
        rkTest.SetValue("TestName", "TestValue");
        Console.WriteLine("Test value for TestName: {0}", rkTest.GetValue("TestName"));
        rkTest.Close();
    } //Main
} //Example


Visual C++

#using <Microsoft.VisualBasic.dll>

using namespace System;
using namespace Microsoft::Win32;
using namespace Microsoft::VisualBasic;

int main()
{
    // Delete and recreate the test key.
    Registry::CurrentUser->DeleteSubKey( L"RegistryOpenSubKeyExample", false );
    RegistryKey ^ rk = Registry::CurrentUser->CreateSubKey( L"RegistryOpenSubKeyExample" );
    rk->Close();

    // Obtain an instance of RegistryKey for the CurrentUser registry
    // root.
    RegistryKey ^ rkCurrentUser = Registry::CurrentUser;

    // Obtain the test key (read-only) and display it.
    RegistryKey ^ rkTest = rkCurrentUser->OpenSubKey( L"RegistryOpenSubKeyExample" );
    Console::WriteLine( L"Test key: {0}", rkTest );
    rkTest->Close();
    rkCurrentUser->Close();

    // Obtain the test key in one step, using the CurrentUser registry
    // root.
    rkTest = Registry::CurrentUser->OpenSubKey( L"RegistryOpenSubKeyExample" );
    Console::WriteLine( L"Test key: {0}", rkTest );
    rkTest->Close();

    // Open the test key in read/write mode.
    rkTest = Registry::CurrentUser->OpenSubKey( L"RegistryOpenSubKeyExample", true );
    rkTest->SetValue( L"TestName", L"TestValue" );
    Console::WriteLine( L"Test value for TestName: {0}", rkTest->GetValue( L"TestName" ) );
    rkTest->Close();

    return 0;
} //Main


Version Information

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
.NET Framework Security

Platforms

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.
See Also

Reference