Skip to content

Commit cac10db

Browse files
2 parents 00d13ae + 277efc7 commit cac10db

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-0
lines changed

DiskCleanup/DiskCleanup.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<Compile Include="Internal\NativeMethods.cs" />
6868
<Compile Include="ProgressCallback.cs" />
6969
<Compile Include="Properties\AssemblyInfo.cs" />
70+
<Compile Include="RecycleBin.cs" />
7071
<Compile Include="UserProfileDirectory.cs" />
7172
<Compile Include="Utilities.cs" />
7273
</ItemGroup>

DiskCleanup/RecycleBin.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Diagnostics.CodeAnalysis;
3+
using System.Runtime.InteropServices;
4+
5+
namespace DiskCleanup
6+
{
7+
public static class RecycleBin
8+
{
9+
[SuppressMessage("ReSharper", "InconsistentNaming")]
10+
private static class NativeMethods
11+
{
12+
public const int SHERB_NOCONFIRMATION = 1;
13+
public const int SHERB_NOPROGRESSUI = 2;
14+
public const int SHERB_NOSOUND = 4;
15+
16+
[DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Auto)]
17+
public static extern int SHEmptyRecycleBinW(IntPtr hwnd, string pszRootPath, uint dwFlags);
18+
}
19+
20+
[Flags]
21+
public enum EmptyOptions : uint
22+
{
23+
Default = NoConfirmation | NoProgressUI | NoSound,
24+
NoConfirmation = NativeMethods.SHERB_NOCONFIRMATION,
25+
NoProgressUI = NativeMethods.SHERB_NOPROGRESSUI,
26+
NoSound = NativeMethods.SHERB_NOSOUND
27+
}
28+
29+
public static void Empty(IntPtr parent = default, string rootPath = null, EmptyOptions options = EmptyOptions.Default)
30+
{
31+
var result = NativeMethods.SHEmptyRecycleBinW(parent, rootPath, (uint) options);
32+
//if (result != 0)
33+
// throw Marshal.GetExceptionForHR(result);
34+
}
35+
}
36+
}

DiskCleanupPS.sln

+28
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,49 @@ EndProject
1212
Global
1313
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1414
Debug|Any CPU = Debug|Any CPU
15+
Debug|x64 = Debug|x64
16+
Debug|x86 = Debug|x86
1517
Release|Any CPU = Release|Any CPU
18+
Release|x64 = Release|x64
19+
Release|x86 = Release|x86
1620
EndGlobalSection
1721
GlobalSection(ProjectConfigurationPlatforms) = postSolution
1822
{839A93F7-0D0D-4FAD-9542-C878F57640E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1923
{839A93F7-0D0D-4FAD-9542-C878F57640E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{839A93F7-0D0D-4FAD-9542-C878F57640E0}.Debug|x64.ActiveCfg = Debug|Any CPU
25+
{839A93F7-0D0D-4FAD-9542-C878F57640E0}.Debug|x64.Build.0 = Debug|Any CPU
26+
{839A93F7-0D0D-4FAD-9542-C878F57640E0}.Debug|x86.ActiveCfg = Debug|Any CPU
27+
{839A93F7-0D0D-4FAD-9542-C878F57640E0}.Debug|x86.Build.0 = Debug|Any CPU
2028
{839A93F7-0D0D-4FAD-9542-C878F57640E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
2129
{839A93F7-0D0D-4FAD-9542-C878F57640E0}.Release|Any CPU.Build.0 = Release|Any CPU
30+
{839A93F7-0D0D-4FAD-9542-C878F57640E0}.Release|x64.ActiveCfg = Release|Any CPU
31+
{839A93F7-0D0D-4FAD-9542-C878F57640E0}.Release|x64.Build.0 = Release|Any CPU
32+
{839A93F7-0D0D-4FAD-9542-C878F57640E0}.Release|x86.ActiveCfg = Release|Any CPU
33+
{839A93F7-0D0D-4FAD-9542-C878F57640E0}.Release|x86.Build.0 = Release|Any CPU
2234
{FD50360A-97D9-4540-B284-01D3739DC435}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2335
{FD50360A-97D9-4540-B284-01D3739DC435}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{FD50360A-97D9-4540-B284-01D3739DC435}.Debug|x64.ActiveCfg = Debug|Any CPU
37+
{FD50360A-97D9-4540-B284-01D3739DC435}.Debug|x64.Build.0 = Debug|Any CPU
38+
{FD50360A-97D9-4540-B284-01D3739DC435}.Debug|x86.ActiveCfg = Debug|Any CPU
39+
{FD50360A-97D9-4540-B284-01D3739DC435}.Debug|x86.Build.0 = Debug|Any CPU
2440
{FD50360A-97D9-4540-B284-01D3739DC435}.Release|Any CPU.ActiveCfg = Release|Any CPU
2541
{FD50360A-97D9-4540-B284-01D3739DC435}.Release|Any CPU.Build.0 = Release|Any CPU
42+
{FD50360A-97D9-4540-B284-01D3739DC435}.Release|x64.ActiveCfg = Release|Any CPU
43+
{FD50360A-97D9-4540-B284-01D3739DC435}.Release|x64.Build.0 = Release|Any CPU
44+
{FD50360A-97D9-4540-B284-01D3739DC435}.Release|x86.ActiveCfg = Release|Any CPU
45+
{FD50360A-97D9-4540-B284-01D3739DC435}.Release|x86.Build.0 = Release|Any CPU
2646
{49A7D490-0181-4FF7-ABD2-0D20F59D1566}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2747
{49A7D490-0181-4FF7-ABD2-0D20F59D1566}.Debug|Any CPU.Build.0 = Debug|Any CPU
48+
{49A7D490-0181-4FF7-ABD2-0D20F59D1566}.Debug|x64.ActiveCfg = Debug|Any CPU
49+
{49A7D490-0181-4FF7-ABD2-0D20F59D1566}.Debug|x64.Build.0 = Debug|Any CPU
50+
{49A7D490-0181-4FF7-ABD2-0D20F59D1566}.Debug|x86.ActiveCfg = Debug|Any CPU
51+
{49A7D490-0181-4FF7-ABD2-0D20F59D1566}.Debug|x86.Build.0 = Debug|Any CPU
2852
{49A7D490-0181-4FF7-ABD2-0D20F59D1566}.Release|Any CPU.ActiveCfg = Release|Any CPU
2953
{49A7D490-0181-4FF7-ABD2-0D20F59D1566}.Release|Any CPU.Build.0 = Release|Any CPU
54+
{49A7D490-0181-4FF7-ABD2-0D20F59D1566}.Release|x64.ActiveCfg = Release|Any CPU
55+
{49A7D490-0181-4FF7-ABD2-0D20F59D1566}.Release|x64.Build.0 = Release|Any CPU
56+
{49A7D490-0181-4FF7-ABD2-0D20F59D1566}.Release|x86.ActiveCfg = Release|Any CPU
57+
{49A7D490-0181-4FF7-ABD2-0D20F59D1566}.Release|x86.Build.0 = Release|Any CPU
3058
EndGlobalSection
3159
GlobalSection(SolutionProperties) = preSolution
3260
HideSolutionNode = FALSE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Diagnostics.CodeAnalysis;
3+
using System.Management.Automation;
4+
using DiskCleanup.Internal;
5+
6+
namespace DiskCleanup.Commands
7+
{
8+
[Cmdlet(VerbsCommon.Remove, "RecycleBinContent")]
9+
[SuppressMessage("ReSharper", "UnusedMember.Global")]
10+
public class RemoveRecycleBinContentCommand : Cmdlet
11+
{
12+
[Parameter(ValueFromPipeline = true)] public string[] PathRoot { get; set; }
13+
[Parameter] public IntPtr Container { get; set; } = default;
14+
[Parameter] public SwitchParameter ShowConfirmation { get; set; }
15+
[Parameter] public SwitchParameter ShowProgressUI { get; set; }
16+
[Parameter] public SwitchParameter PlaySound { get; set; }
17+
18+
protected override void ProcessRecord()
19+
{
20+
base.ProcessRecord();
21+
22+
var options = RecycleBin.EmptyOptions.Default;
23+
24+
if (ShowConfirmation)
25+
options ^= RecycleBin.EmptyOptions.NoConfirmation;
26+
if (ShowProgressUI)
27+
options ^= RecycleBin.EmptyOptions.NoProgressUI;
28+
if (PlaySound)
29+
options ^= RecycleBin.EmptyOptions.NoSound;
30+
31+
if (PathRoot == null)
32+
{
33+
try
34+
{
35+
RecycleBin.Empty(Container, options: options);
36+
}
37+
catch (Exception e)
38+
{
39+
WriteError(e.ToErrorRecord());
40+
}
41+
42+
return;
43+
}
44+
45+
foreach (var path in PathRoot)
46+
{
47+
try
48+
{
49+
RecycleBin.Empty(Container, path, options);
50+
}
51+
catch (Exception e)
52+
{
53+
WriteError(e.ToErrorRecord());
54+
}
55+
}
56+
}
57+
}
58+
}

DiskCleanupPSModule/DiskCleanupPSModule.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<Compile Include="Commands\GetFileSystemItemSize.cs" />
5757
<Compile Include="Commands\GetUserProfileCommand.cs" />
5858
<Compile Include="Commands\NewNtfsCompressionOptions.cs" />
59+
<Compile Include="Commands\RemoveRecycleBinContentCommand.cs" />
5960
<Compile Include="Commands\RemoveTempFilesCommand.cs" />
6061
<Compile Include="Commands\SetNtfsCompressionCommand.cs" />
6162
<Compile Include="Commands\TempFileScope.cs" />

Tests/Program.cs

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Diagnostics;
33
using System.IO;
44
using System.Threading;
5+
using DiskCleanup;
56
using DiskCleanup.Compression;
67

78
namespace diskcleaner
@@ -10,6 +11,8 @@ internal class Program
1011
{
1112
private static void Main(string[] args)
1213
{
14+
RecycleBin.Empty();
15+
1316
var cancel = 0;
1417
Console.CancelKeyPress += (sender, eventArgs) =>
1518
{

0 commit comments

Comments
 (0)