Creates a new subkey or opens an existing subkey for write access. The string subkey is not case-sensitive.
Namespace:
Microsoft.Win32
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Function CreateSubKey ( _
subkey As String _
) As RegistryKey
Dim instance As RegistryKey
Dim subkey As String
Dim returnValue As RegistryKey
returnValue = instance.CreateSubKey(subkey)
public RegistryKey CreateSubKey(
string subkey
)
public:
RegistryKey^ CreateSubKey(
String^ subkey
)
public function CreateSubKey(
subkey : String
) : RegistryKey
Parameters
- subkey
- Type: System..::.String
The name or path of the subkey to create or open.
Return Value
Type:
Microsoft.Win32..::.RegistryKeyA RegistryKey object that represents the existing or newly created subkey, or nullNothingnullptra null reference (Nothing in Visual Basic) if the operation failed. If a zero-length string is specified for subkey, the current RegistryKey object is returned.
In order to perform this action, the user must have permission at this level and below in the registry hierarchy.
Caution: |
|---|
Do not expose RegistryKey objects in such a way that a malicious program could create thousands of meaningless subkeys or key/value pairs. For example, do not allow callers to enter arbitrary keys or values. |
The following code example shows how to create a subkey under HKEY_CURRENT_USER, manipulate its contents, and then delete the subkey.
Imports Microsoft.VisualBasic
Imports System
Imports System.Security.Permissions
Imports Microsoft.Win32
<Assembly: RegistryPermissionAttribute( _
SecurityAction.RequestMinimum, ViewAndModify := "HKEY_CURRENT_USER")>
Public Class RegKey
Shared Sub Main()
' Create a subkey named Test9999 under HKEY_CURRENT_USER.
Dim test9999 As RegistryKey = _
Registry.CurrentUser.CreateSubKey("Test9999")
' Create two subkeys under HKEY_CURRENT_USER\Test9999.
test9999.CreateSubKey("TestName").Close()
Dim testSettings As RegistryKey = _
test9999.CreateSubKey("TestSettings")
' Create data for the TestSettings subkey.
testSettings.SetValue("Language", "French")
testSettings.SetValue("Level", "Intermediate")
testSettings.SetValue("ID", 123)
testSettings.Close()
' Print the information from the Test9999 subkey.
Console.WriteLine("There are {0} subkeys under Test9999.", _
test9999.SubKeyCount.ToString())
For Each subKeyName As String In test9999.GetSubKeyNames()
Dim tempKey As RegistryKey = _
test9999.OpenSubKey(subKeyName)
Console.WriteLine(vbCrLf & "There are {0} values for " & _
"{1}.", tempKey.ValueCount.ToString(), tempKey.Name)
For Each valueName As String In tempKey.GetValueNames()
Console.WriteLine("{0,-8}: {1}", valueName, _
tempKey.GetValue(valueName).ToString())
Next
Next
' Delete the ID value.
testSettings = test9999.OpenSubKey("TestSettings", True)
testSettings.DeleteValue("id")
' Verify the deletion.
Console.WriteLine(CType(testSettings.GetValue( _
"id", "ID not found."), String))
testSettings.Close()
' Delete or close the new subkey.
Console.Write(vbCrLf & "Delete newly created " & _
"registry key? (Y/N) ")
If Char.ToUpper(Convert.ToChar(Console.Read())) = "Y"C Then
Registry.CurrentUser.DeleteSubKeyTree("Test9999")
Console.WriteLine(vbCrLf & "Registry key {0} deleted.", _
test9999.Name)
Else
Console.WriteLine(vbCrLf & "Registry key {0} closed.", _
test9999.ToString())
test9999.Close()
End If
End Sub
End Class
using System;
using System.Security.Permissions;
using Microsoft.Win32;
[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum,
ViewAndModify = "HKEY_CURRENT_USER")]
class RegKey
{
static void Main()
{
// Create a subkey named Test9999 under HKEY_CURRENT_USER.
RegistryKey test9999 =
Registry.CurrentUser.CreateSubKey("Test9999");
// Create two subkeys under HKEY_CURRENT_USER\Test9999. The
// keys are disposed when execution exits the using statement.
using(RegistryKey
testName = test9999.CreateSubKey("TestName"),
testSettings = test9999.CreateSubKey("TestSettings"))
{
// Create data for the TestSettings subkey.
testSettings.SetValue("Language", "French");
testSettings.SetValue("Level", "Intermediate");
testSettings.SetValue("ID", 123);
}
// Print the information from the Test9999 subkey.
Console.WriteLine("There are {0} subkeys under {1}.",
test9999.SubKeyCount.ToString(), test9999.Name);
foreach(string subKeyName in test9999.GetSubKeyNames())
{
using(RegistryKey
tempKey = test9999.OpenSubKey(subKeyName))
{
Console.WriteLine("\nThere are {0} values for {1}.",
tempKey.ValueCount.ToString(), tempKey.Name);
foreach(string valueName in tempKey.GetValueNames())
{
Console.WriteLine("{0,-8}: {1}", valueName,
tempKey.GetValue(valueName).ToString());
}
}
}
using(RegistryKey
testSettings = test9999.OpenSubKey("TestSettings", true))
{
// Delete the ID value.
testSettings.DeleteValue("id");
// Verify the deletion.
Console.WriteLine((string)testSettings.GetValue(
"id", "ID not found."));
}
// Delete or close the new subkey.
Console.Write("\nDelete newly created registry key? (Y/N) ");
if(Char.ToUpper(Convert.ToChar(Console.Read())) == 'Y')
{
Registry.CurrentUser.DeleteSubKeyTree("Test9999");
Console.WriteLine("\nRegistry key {0} deleted.",
test9999.Name);
}
else
{
Console.WriteLine("\nRegistry key {0} closed.",
test9999.ToString());
test9999.Close();
}
}
}
using namespace System;
using namespace System::Security::Permissions;
using namespace Microsoft::Win32;
[assembly:RegistryPermissionAttribute(SecurityAction::RequestMinimum,
ViewAndModify="HKEY_CURRENT_USER")];
int main()
{
// Create a subkey named Test9999 under HKEY_CURRENT_USER.
RegistryKey ^ test9999 = Registry::CurrentUser->CreateSubKey( "Test9999" );
// Create two subkeys under HKEY_CURRENT_USER\Test9999.
test9999->CreateSubKey( "TestName" )->Close();
RegistryKey ^ testSettings = test9999->CreateSubKey( "TestSettings" );
// Create data for the TestSettings subkey.
testSettings->SetValue( "Language", "French" );
testSettings->SetValue( "Level", "Intermediate" );
testSettings->SetValue( "ID", 123 );
testSettings->Close();
// Print the information from the Test9999 subkey.
Console::WriteLine( "There are {0} subkeys under Test9999.", test9999->SubKeyCount.ToString() );
array<String^>^subKeyNames = test9999->GetSubKeyNames();
for ( int i = 0; i < subKeyNames->Length; i++ )
{
RegistryKey ^ tempKey = test9999->OpenSubKey( subKeyNames[ i ] );
Console::WriteLine( "\nThere are {0} values for {1}.", tempKey->ValueCount.ToString(), tempKey->Name );
array<String^>^valueNames = tempKey->GetValueNames();
for ( int j = 0; j < valueNames->Length; j++ )
{
Console::WriteLine( "{0,-8}: {1}", valueNames[ j ], tempKey->GetValue( valueNames[ j ] )->ToString() );
}
}
// Delete the ID value.
testSettings = test9999->OpenSubKey( "TestSettings", true );
testSettings->DeleteValue( "id" );
// Verify the deletion.
Console::WriteLine( dynamic_cast<String^>(testSettings->GetValue( "id", "ID not found." )) );
testSettings->Close();
// Delete or close the new subkey.
Console::Write( "\nDelete newly created registry key? (Y/N) " );
if ( Char::ToUpper( Convert::ToChar( Console::Read() ) ) == 'Y' )
{
Registry::CurrentUser->DeleteSubKeyTree( "Test9999" );
Console::WriteLine( "\nRegistry key {0} deleted.", test9999->Name );
}
else
{
Console::WriteLine( "\nRegistry key {0} closed.", test9999->ToString() );
test9999->Close();
}
}
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 3.5, 2.0
Reference