private void DeleteSubKeyTreeInternal(string subkey)
        {
            int errorCode = 0;
            SafeTransactionHandle transactionHandle = this.GetTransactionHandle();
            TransactedRegistryKey key = this.InternalOpenSubKey(subkey, true);

            if (key == null)
            {
                throw new ArgumentException(RegistryProviderStrings.Arg_RegSubKeyAbsent);
            }
            try
            {
                if (key.InternalSubKeyCount() > 0)
                {
                    string[] subKeyNames = key.InternalGetSubKeyNames();
                    for (int i = 0; i < subKeyNames.Length; i++)
                    {
                        key.DeleteSubKeyTreeInternal(subKeyNames[i]);
                    }
                }
            }
            finally
            {
                key.Close();
            }
            errorCode = Microsoft.PowerShell.Commands.Internal.Win32Native.RegDeleteKeyTransacted(this.hkey, subkey, 0, 0, transactionHandle, IntPtr.Zero);
            if (errorCode != 0)
            {
                this.Win32Error(errorCode, null);
            }
        }
        public void DeleteSubKey(string subkey, bool throwOnMissingSubKey)
        {
            ValidateKeyName(subkey);
            this.EnsureWriteable();
            subkey = FixupName(subkey);
            this.CheckSubKeyWritePermission(subkey);
            TransactedRegistryKey key = this.InternalOpenSubKey(subkey, false);

            if (key != null)
            {
                try
                {
                    if (key.InternalSubKeyCount() > 0)
                    {
                        throw new InvalidOperationException(RegistryProviderStrings.InvalidOperation_RegRemoveSubKey);
                    }
                }
                finally
                {
                    key.Close();
                }
                int errorCode = 0;
                SafeTransactionHandle transactionHandle = this.GetTransactionHandle();
                errorCode = Microsoft.PowerShell.Commands.Internal.Win32Native.RegDeleteKeyTransacted(this.hkey, subkey, 0, 0, transactionHandle, IntPtr.Zero);
                switch (errorCode)
                {
                case 0:
                    return;

                case 2:
                    if (throwOnMissingSubKey)
                    {
                        throw new ArgumentException(RegistryProviderStrings.ArgumentException_RegSubKeyAbsent);
                    }
                    return;
                }
                this.Win32Error(errorCode, null);
            }
            else if (throwOnMissingSubKey)
            {
                throw new ArgumentException(RegistryProviderStrings.ArgumentException_RegSubKeyAbsent);
            }
        }
        public void DeleteSubKeyTree(string subkey)
        {
            ValidateKeyName(subkey);
            if ((string.IsNullOrEmpty(subkey) || (subkey.Length == 0)) && this.IsSystemKey())
            {
                throw new ArgumentException(RegistryProviderStrings.ArgRegKeyDelHive);
            }
            this.EnsureWriteable();
            int errorCode = 0;
            SafeTransactionHandle transactionHandle = this.GetTransactionHandle();

            subkey = FixupName(subkey);
            this.CheckSubTreeWritePermission(subkey);
            TransactedRegistryKey key = this.InternalOpenSubKey(subkey, true);

            if (key == null)
            {
                throw new ArgumentException(RegistryProviderStrings.Arg_RegSubKeyAbsent);
            }
            try
            {
                if (key.InternalSubKeyCount() > 0)
                {
                    string[] subKeyNames = key.InternalGetSubKeyNames();
                    for (int i = 0; i < subKeyNames.Length; i++)
                    {
                        key.DeleteSubKeyTreeInternal(subKeyNames[i]);
                    }
                }
            }
            finally
            {
                key.Close();
            }
            errorCode = Microsoft.PowerShell.Commands.Internal.Win32Native.RegDeleteKeyTransacted(this.hkey, subkey, 0, 0, transactionHandle, IntPtr.Zero);
            if (errorCode != 0)
            {
                this.Win32Error(errorCode, null);
            }
        }