void DeleteSubKeyTree(string subkey, bool throwOnMissingSubKey) { // Note: this is done by deleting sub-nodes recursively. // The preformance is not very good. There may be a // better way to implement this. AssertKeyStillValid(); AssertKeyNameNotNull(subkey); AssertKeyNameLength(subkey); RegistryKey child = OpenSubKey(subkey, true); if (child == null) { if (!throwOnMissingSubKey) { return; } throw new ArgumentException("Cannot delete a subkey tree" + " because the subkey does not exist."); } child.DeleteChildKeysAndValues(); child.Close(); DeleteSubKey(subkey, false); }
/// <summary> /// Utility method to delelte a key's sub keys and values. /// This method removes a level of indirection when deleting /// key node trees. /// </summary> private void DeleteChildKeysAndValues() { if (IsRoot) { return; } string[] subKeys = GetSubKeyNames(); foreach (string subKey in subKeys) { RegistryKey sub = OpenSubKey(subKey, true); sub.DeleteChildKeysAndValues(); sub.Close(); DeleteSubKey(subKey, false); } string[] values = GetValueNames(); foreach (string value in values) { DeleteValue(value, false); } }