Add back Clear-RecycleBin for Windows (#10909)

This commit is contained in:
Steve Lee
2019-10-28 08:18:26 +05:00
committed by Ilya
parent 3c432ad288
commit ca1b6fb4c5
4 changed files with 7 additions and 17 deletions
@@ -15,7 +15,6 @@
</PropertyGroup>
<ItemGroup>
<Compile Remove="commands\management\ClearRecycleBinCommand.cs" />
<Compile Remove="commands\management\ControlPanelItemCommand.cs" />
<Compile Remove="commands\management\CommitTransactionCommand.cs" />
<Compile Remove="commands\management\Eventlog.cs" />
@@ -39,7 +38,6 @@
<Compile Remove="gen\ControlPanelResources.cs" />
<Compile Remove="gen\WmiResources.cs" />
<Compile Remove="gen\ManagementMshSnapInResources.cs" />
<Compile Remove="gen\ClearRecycleBinResources.cs" />
<Compile Remove="gen\ClipboardResources.cs" />
<EmbeddedResource Remove="resources\EventlogResources.resx" />
@@ -48,7 +46,6 @@
<EmbeddedResource Remove="resources\ControlPanelResources.resx" />
<EmbeddedResource Remove="resources\WmiResources.resx" />
<EmbeddedResource Remove="resources\ManagementMshSnapInResources.resx" />
<EmbeddedResource Remove="resources\ClearRecycleBinResources.resx" />
<EmbeddedResource Remove="resources\ClipboardResources.resx" />
</ItemGroup>
@@ -10,6 +10,8 @@ using System.Management.Automation;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
#if !UNIX
namespace Microsoft.PowerShell.Commands
{
/// <summary>
@@ -224,25 +226,14 @@ namespace Microsoft.PowerShell.Commands
progress.RecordType = ProgressRecordType.Processing;
WriteProgress(progress);
// no need to check result as a failure is returned only if recycle bin is already empty
uint result = NativeMethod.SHEmptyRecycleBin(IntPtr.Zero, drivePath,
NativeMethod.RecycleFlags.SHERB_NOCONFIRMATION |
NativeMethod.RecycleFlags.SHERB_NOPROGRESSUI |
NativeMethod.RecycleFlags.SHERB_NOSOUND);
int lastError = Marshal.GetLastWin32Error();
// update the progress bar to completed
progress.PercentComplete = 100;
progress.RecordType = ProgressRecordType.Completed;
WriteProgress(progress);
// 0 is for a successful operation
// 203 comes up when trying to empty an already emptied recyclebin
// 18 comes up when there are no more files in the given recyclebin
if (!(lastError == 0 || lastError == 203 || lastError == 18))
{
Win32Exception exception = new Win32Exception(lastError);
WriteError(new ErrorRecord(exception, "FailedToClearRecycleBin", ErrorCategory.InvalidOperation, "RecycleBin"));
}
}
}
}
@@ -261,3 +252,4 @@ namespace Microsoft.PowerShell.Commands
internal static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags);
}
}
#endif
@@ -67,5 +67,6 @@ CmdletsToExport=@("Add-Content",
"Get-ComputerInfo",
"Get-TimeZone",
"Set-TimeZone",
"Get-HotFix")
"Get-HotFix",
"Clear-RecycleBin")
}
@@ -195,7 +195,7 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Cmdlet", "Clear-History", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "Clear-Item", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "Clear-ItemProperty", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "Clear-RecycleBin", "", $($FullCLR ), "", "", ""
"Cmdlet", "Clear-RecycleBin", "", $($FullCLR -or $CoreWindows ), "", "", "High"
"Cmdlet", "Clear-Variable", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "Compare-Object", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Complete-Transaction", "", $($FullCLR ), "", "", ""