RegistryKey.OpenSubKey Method (String)
Assembly: mscorlib (in mscorlib.dll)
| Exception type | Condition |
|---|---|
| name is a null reference (Nothing in Visual Basic) | |
| name is longer than the maximum length allowed (255 characters). | |
| The RegistryKey is closed (closed keys cannot be accessed). | |
| 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
- RegistryPermission for the ability to read the specified registry key. Associated enumeration: RegistryPermissionAccess.Read
- SecurityPermission for the ability to access the specified registry key if it is a remote key. Associated enumeration: SecurityPermissionFlag.UnmanagedCode
Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.