RegistryKey.DeleteSubKey Method

Definition

Deletes the specified subkey.

Overloads

DeleteSubKey(String)

Deletes the specified subkey.

DeleteSubKey(String, Boolean)

Deletes the specified subkey, and specifies whether an exception is raised if the subkey is not found.

DeleteSubKey(String)

Source:
RegistryKey.cs

Deletes the specified subkey.

public:
 void DeleteSubKey(System::String ^ subkey);
public void DeleteSubKey (string subkey);
member this.DeleteSubKey : string -> unit
Public Sub DeleteSubKey (subkey As String)

Parameters

subkey
String

The name of the subkey to delete. This string is not case-sensitive.

Exceptions

The subkey has child subkeys.

The subkey parameter does not specify a valid registry key.

subkey is null

The user does not have the permissions required to delete the key.

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

The user does not have the necessary registry rights.

Examples

The following example demonstrates how to use DeleteSubKey.

using namespace System;
using namespace Microsoft::Win32;

public ref class RegKeyDel
{
public:
    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.
        RegistryKey^ testName = test9999->CreateSubKey("TestName");
        RegistryKey^ testSettings = test9999->CreateSubKey("TestSettings");

        // Create data for the TestSettings subkey.
        testSettings->SetValue("Language", "French");
        testSettings->SetValue("Level", "Intermediate");
        testSettings->SetValue("ID", 123);

        // delete the subkey "TestName"
        test9999->DeleteSubKey("TestName");
        // delete everything under and including "Test9999"
        Registry::CurrentUser->DeleteSubKeyTree("Test9999");
    }
};

int main()
{
    RegKeyDel::Main();
}
using System;
using Microsoft.Win32;

class RegKeyDel
{
    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.
        RegistryKey testName = test9999.CreateSubKey("TestName");
        RegistryKey testSettings = test9999.CreateSubKey("TestSettings");

        // Create data for the TestSettings subkey.
        testSettings.SetValue("Language", "French");
        testSettings.SetValue("Level", "Intermediate");
        testSettings.SetValue("ID", 123);

        // delete the subkey "TestName"
        test9999.DeleteSubKey("TestName");
        // delete everything under and including "Test9999"
        Registry.CurrentUser.DeleteSubKeyTree("Test9999");
    }
}
Imports Microsoft.Win32

Public Class RegKeyDel
    Public 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. The
        ' keys are disposed when execution exits the using statement.
        Dim testName As RegistryKey = test9999.CreateSubKey("TestName")
        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)

        ' delete the subkey "TestName"
        test9999.DeleteSubKey("TestName")
        ' delete everything under and including "Test9999"
        Registry.CurrentUser.DeleteSubKeyTree("Test9999")
    End Sub
End Class

Remarks

To delete child subkeys, use DeleteSubKeyTree.

Use caution when deleting registry keys.

See also

Applies to

DeleteSubKey(String, Boolean)

Source:
RegistryKey.cs

Deletes the specified subkey, and specifies whether an exception is raised if the subkey is not found.

public:
 void DeleteSubKey(System::String ^ subkey, bool throwOnMissingSubKey);
public void DeleteSubKey (string subkey, bool throwOnMissingSubKey);
member this.DeleteSubKey : string * bool -> unit
Public Sub DeleteSubKey (subkey As String, throwOnMissingSubKey As Boolean)

Parameters

subkey
String

The name of the subkey to delete. This string is not case-sensitive.

throwOnMissingSubKey
Boolean

Indicates whether an exception should be raised if the specified subkey cannot be found. If this argument is true and the specified subkey does not exist, an exception is raised. If this argument is false and the specified subkey does not exist, no action is taken.

Exceptions

subkey has child subkeys.

subkey does not specify a valid registry key, and throwOnMissingSubKey is true.

subkey is null.

The user does not have the permissions required to delete the key.

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

The user does not have the necessary registry rights.

Remarks

To delete child subkeys, use DeleteSubKeyTree.

Use caution when deleting registry keys.

See also

Applies to