Export (0) Print
Expand All

RegistryKey.OpenSubKey Method (String)

Retrieves a subkey as read-only.

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

public RegistryKey OpenSubKey (
	string name
)
public RegistryKey OpenSubKey (
	String name
)
public function OpenSubKey (
	name : String
) : RegistryKey
Not applicable.

Parameters

name

The name or path of the subkey to open read-only.

Return Value

The subkey requested, or a null reference (Nothing in Visual Basic) if the operation failed.

Exception typeCondition

ArgumentNullException

name is a null reference (Nothing in Visual Basic)

ArgumentException

name is longer than the maximum length allowed (255 characters).

ObjectDisposedException

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

SecurityException

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

You must open a key before it can be manipulated with other methods and properties. To modify a key, you must open it with an overload of the OpenSubKey method that allows you to specify write access, such as the OpenSubKey(String,RegistryKeyPermissionCheck) overload or the OpenSubKey(String,Boolean) overload.

If the specified subkey cannot be found, then a null reference (Nothing in Visual Basic) is returned.

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

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

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

import System.*;
import Microsoft.Win32.*;
import Microsoft.VisualBasic.*;

public class Example
{
    public static void main(String[] args)
    {
        // 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 

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft