-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
ClearRecycleBinCommand.cs
263 lines (237 loc) · 10.1 KB
/
ClearRecycleBinCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Management.Automation;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Defines the implementation of the 'Clear-RecycleBin' cmdlet.
/// This cmdlet clear all files in the RecycleBin for the given DriveLetter.
/// If not DriveLetter is specified, then the RecycleBin for all drives are cleared.
/// </summary>
[Cmdlet(VerbsCommon.Clear, "RecycleBin", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=524082", ConfirmImpact = ConfirmImpact.High)]
public class ClearRecycleBinCommand : PSCmdlet
{
private string[] _drivesList;
private DriveInfo[] _availableDrives;
private bool _force;
/// <summary>
/// Property that sets DriveLetter parameter.
/// </summary>
[Parameter(Position = 0, ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public string[] DriveLetter
{
get { return _drivesList; }
set { _drivesList = value; }
}
/// <summary>
/// Property that sets force parameter. This will allow to clear the recyclebin.
/// </summary>
[Parameter()]
public SwitchParameter Force
{
get
{
return _force;
}
set
{
_force = value;
}
}
/// <summary>
/// This method implements the BeginProcessing method for Clear-RecycleBin command.
/// </summary>
protected override void BeginProcessing()
{
_availableDrives = DriveInfo.GetDrives();
}
/// <summary>
/// This method implements the ProcessRecord method for Clear-RecycleBin command.
/// </summary>
protected override void ProcessRecord()
{
// There are two scenarios:
// 1) The user provides a list of drives.
if (_drivesList != null)
{
foreach (var drive in _drivesList)
{
if (!IsValidPattern(drive))
{
WriteError(new ErrorRecord(
new ArgumentException(
string.Format(CultureInfo.InvariantCulture, ClearRecycleBinResources.InvalidDriveNameFormat, "C", "C:", "C:\\")),
"InvalidDriveNameFormat",
ErrorCategory.InvalidArgument,
drive));
continue;
}
// Get the full path for the drive.
string drivePath = GetDrivePath(drive);
if (ValidDrivePath(drivePath))
{
EmptyRecycleBin(drivePath);
}
}
}
else
{
// 2) No drivesList is provided by the user.
EmptyRecycleBin(null);
}
}
/// <summary>
/// Returns true if the given drive is 'fixed' and its path exist; otherwise, return false.
/// </summary>
/// <param name="drivePath"></param>
/// <returns></returns>
private bool ValidDrivePath(string drivePath)
{
DriveInfo actualDrive = null;
if (_availableDrives != null)
{
foreach (DriveInfo drive in _availableDrives)
{
if (string.Compare(drive.Name, drivePath, StringComparison.OrdinalIgnoreCase) == 0)
{
actualDrive = drive;
break;
}
}
}
// The drive was not found.
if (actualDrive == null)
{
WriteError(new ErrorRecord(
new System.IO.DriveNotFoundException(
string.Format(CultureInfo.InvariantCulture, ClearRecycleBinResources.DriveNotFound, drivePath, "Get-Volume")),
"DriveNotFound",
ErrorCategory.InvalidArgument,
drivePath));
}
else
{
if (actualDrive.DriveType == DriveType.Fixed)
{
// The drive path exists, and the drive is 'fixed'.
return true;
}
WriteError(new ErrorRecord(
new ArgumentException(
string.Format(CultureInfo.InvariantCulture, ClearRecycleBinResources.InvalidDriveType, drivePath, "Get-Volume")),
"InvalidDriveType",
ErrorCategory.InvalidArgument,
drivePath));
}
return false;
}
/// <summary>
/// Returns true if the given input is of the form c, c:, c:\, C, C: or C:\
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private bool IsValidPattern(string input)
{
return Regex.IsMatch(input, @"^[a-z]{1}$|^[a-z]{1}:$|^[a-z]{1}:\\$", RegexOptions.IgnoreCase);
}
/// <summary>
/// Returns a drive path of the form C:\ for the given drive driveName.
/// Supports the following inputs: C, C:, C:\
/// </summary>
/// <param name="driveName"></param>
/// <returns></returns>
private string GetDrivePath(string driveName)
{
string drivePath;
if (driveName.EndsWith(":\\", StringComparison.OrdinalIgnoreCase))
{
drivePath = driveName;
}
else if (driveName.EndsWith(':'))
{
drivePath = driveName + "\\";
}
else
{
drivePath = driveName + ":\\";
}
return drivePath;
}
/// <summary>
/// Clear the recyclebin for the given drive name.
/// If no driveName is provided, it clears the recyclebin for all drives.
/// </summary>
/// <param name="drivePath"></param>
private void EmptyRecycleBin(string drivePath)
{
string clearRecycleBinShouldProcessTarget;
if (drivePath == null)
{
clearRecycleBinShouldProcessTarget = string.Format(CultureInfo.InvariantCulture,
ClearRecycleBinResources.ClearRecycleBinContent);
}
else
{
clearRecycleBinShouldProcessTarget = string.Format(CultureInfo.InvariantCulture,
ClearRecycleBinResources.ClearRecycleBinContentForDrive,
drivePath);
}
if (_force || (ShouldProcess(clearRecycleBinShouldProcessTarget, "Clear-RecycleBin")))
{
// If driveName is null, then clear the recyclebin for all drives; otherwise, just for the specified driveName.
string activity = string.Format(CultureInfo.InvariantCulture, ClearRecycleBinResources.ClearRecycleBinProgressActivity);
string statusDescription;
if (drivePath == null)
{
statusDescription = string.Format(CultureInfo.InvariantCulture, ClearRecycleBinResources.ClearRecycleBinStatusDescriptionForAllDrives);
}
else
{
statusDescription = string.Format(CultureInfo.InvariantCulture, ClearRecycleBinResources.ClearRecycleBinStatusDescriptionByDrive, drivePath);
}
ProgressRecord progress = new ProgressRecord(0, activity, statusDescription);
progress.PercentComplete = 30;
progress.RecordType = ProgressRecordType.Processing;
WriteProgress(progress);
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"));
}
}
}
}
internal static class NativeMethod
{
// Internal code to SHEmptyRecycleBin
internal enum RecycleFlags : uint
{
SHERB_NOCONFIRMATION = 0x00000001,
SHERB_NOPROGRESSUI = 0x00000002,
SHERB_NOSOUND = 0x00000004
}
[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
internal static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags);
}
}