RegistryKey::OpenSubKey Method (String^, Boolean)
Retrieves a specified subkey, and specifies whether write access is to be applied to the key.
Assembly: mscorlib (in mscorlib.dll)
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.
| 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. |
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.
The following code example creates a test key and uses the OpenSubKey method to open it, demonstrating both overloads of the method.
#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
for the ability to read the specified registry key. Associated enumeration: RegistryPermissionAccess::Read
for the ability to access the specified registry key if it is a remote key. Associated enumeration: SecurityPermissionFlag::UnmanagedCode
Available since 1.1