RegistryKey.DeleteSubKeyTree Method

Definition

Deletes a subkey and any child subkeys recursively, with optional exception handling.

Overloads

DeleteSubKeyTree(String, Boolean)

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

DeleteSubKeyTree(String)

Deletes a subkey and any child subkeys recursively.

DeleteSubKeyTree(String, Boolean)

Source:
RegistryKey.cs

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

C#
public void DeleteSubKeyTree(string subkey, bool throwOnMissingSubKey);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public void DeleteSubKeyTree(string subkey, bool throwOnMissingSubKey);

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.

Attributes

Exceptions

An attempt was made to delete the root hive of the tree.

-or-

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

subkey is null.

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

The user does not have the necessary registry rights.

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5

DeleteSubKeyTree(String)

Source:
RegistryKey.cs

Deletes a subkey and any child subkeys recursively.

C#
public void DeleteSubKeyTree(string subkey);

Parameters

subkey
String

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

Exceptions

subkey is null.

Deletion of a root hive is attempted.

-or-

subkey does not specify a valid registry subkey.

An I/O error has occurred.

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

This code example is part of a larger example provided for the RegistryKey class.

C#
// 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();
}

Remarks

You must have appropriate permissions to delete the subkey and its tree.

Caution

Deleting a particular key will remove all entries below the key in the tree. No warning will be provided. If you want to delete a subkey only when it has no child subkeys, use the DeleteSubKey method.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5