RegistryKey.DeleteSubKey RegistryKey.DeleteSubKey RegistryKey.DeleteSubKey RegistryKey.DeleteSubKey Method

Definition

Deletes the specified subkey.

Overloads

DeleteSubKey(String) DeleteSubKey(String) DeleteSubKey(String) DeleteSubKey(String)

Deletes the specified subkey.

DeleteSubKey(String, Boolean) DeleteSubKey(String, Boolean) DeleteSubKey(String, Boolean) DeleteSubKey(String, Boolean)

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

DeleteSubKey(String) DeleteSubKey(String) DeleteSubKey(String) DeleteSubKey(String)

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 String String String

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

Exceptions

The subkey parameter does not specify a valid registry key

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

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

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 System
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.

Security

RegistryPermission
for the ability to modify the specified registry key. Associated enumeration: Write

SecurityPermission
for the ability to access the specified registry key if it is a remote key. Associated enumeration: UnmanagedCode

See Also

DeleteSubKey(String, Boolean) DeleteSubKey(String, Boolean) DeleteSubKey(String, Boolean) DeleteSubKey(String, Boolean)

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 String String String

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

throwOnMissingSubKey
Boolean Boolean Boolean 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 does not specify a valid registry key, and throwOnMissingSubKey is true.

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

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

Remarks

To delete child subkeys, use DeleteSubKeyTree.

Use caution when deleting registry keys.

Security

RegistryPermission
for the ability to modify the specified registry key. Associated enumeration: Write

SecurityPermission
for the ability to access the specified registry key if it is a remote key. Associated enumeration: UnmanagedCode

See Also

Applies to