├── .gitignore
├── LICENSE
├── README.md
├── Screenshot.png
└── SharpDiskSweeper
├── DiskSweeper.sln
└── DiskSweeper
├── App.config
├── App.xaml
├── App.xaml.cs
├── ConfigurationHelper.cs
├── DiskItem.cs
├── DiskSweeper.csproj
├── DiskSweeper.ico
├── FileInfoExtensions.cs
├── GridViewSort.cs
├── MainWindow.xaml
├── MainWindow.xaml.cs
├── Properties
├── AssemblyInfo.cs
├── Resources.Designer.cs
├── Resources.resx
├── Settings.Designer.cs
└── Settings.settings
├── Resources
├── File.png
└── Folder.png
├── ShellExtensionAdder.cs
└── SweepEngine.cs
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.suo
8 | *.user
9 | *.userosscache
10 | *.sln.docstates
11 |
12 | # User-specific files (MonoDevelop/Xamarin Studio)
13 | *.userprefs
14 |
15 | # Build results
16 | [Dd]ebug/
17 | [Dd]ebugPublic/
18 | [Rr]elease/
19 | [Rr]eleases/
20 | x64/
21 | x86/
22 | bld/
23 | [Bb]in/
24 | [Oo]bj/
25 | [Ll]og/
26 |
27 | # Visual Studio 2015/2017 cache/options directory
28 | .vs/
29 | # Uncomment if you have tasks that create the project's static files in wwwroot
30 | #wwwroot/
31 |
32 | # Visual Studio 2017 auto generated files
33 | Generated\ Files/
34 |
35 | # MSTest test Results
36 | [Tt]est[Rr]esult*/
37 | [Bb]uild[Ll]og.*
38 |
39 | # NUNIT
40 | *.VisualState.xml
41 | TestResult.xml
42 |
43 | # Build Results of an ATL Project
44 | [Dd]ebugPS/
45 | [Rr]eleasePS/
46 | dlldata.c
47 |
48 | # Benchmark Results
49 | BenchmarkDotNet.Artifacts/
50 |
51 | # .NET Core
52 | project.lock.json
53 | project.fragment.lock.json
54 | artifacts/
55 | **/Properties/launchSettings.json
56 |
57 | # StyleCop
58 | StyleCopReport.xml
59 |
60 | # Files built by Visual Studio
61 | *_i.c
62 | *_p.c
63 | *_i.h
64 | *.ilk
65 | *.meta
66 | *.obj
67 | *.iobj
68 | *.pch
69 | *.pdb
70 | *.ipdb
71 | *.pgc
72 | *.pgd
73 | *.rsp
74 | *.sbr
75 | *.tlb
76 | *.tli
77 | *.tlh
78 | *.tmp
79 | *.tmp_proj
80 | *.log
81 | *.vspscc
82 | *.vssscc
83 | .builds
84 | *.pidb
85 | *.svclog
86 | *.scc
87 |
88 | # Chutzpah Test files
89 | _Chutzpah*
90 |
91 | # Visual C++ cache files
92 | ipch/
93 | *.aps
94 | *.ncb
95 | *.opendb
96 | *.opensdf
97 | *.sdf
98 | *.cachefile
99 | *.VC.db
100 | *.VC.VC.opendb
101 |
102 | # Visual Studio profiler
103 | *.psess
104 | *.vsp
105 | *.vspx
106 | *.sap
107 |
108 | # Visual Studio Trace Files
109 | *.e2e
110 |
111 | # TFS 2012 Local Workspace
112 | $tf/
113 |
114 | # Guidance Automation Toolkit
115 | *.gpState
116 |
117 | # ReSharper is a .NET coding add-in
118 | _ReSharper*/
119 | *.[Rr]e[Ss]harper
120 | *.DotSettings.user
121 |
122 | # JustCode is a .NET coding add-in
123 | .JustCode
124 |
125 | # TeamCity is a build add-in
126 | _TeamCity*
127 |
128 | # DotCover is a Code Coverage Tool
129 | *.dotCover
130 |
131 | # AxoCover is a Code Coverage Tool
132 | .axoCover/*
133 | !.axoCover/settings.json
134 |
135 | # Visual Studio code coverage results
136 | *.coverage
137 | *.coveragexml
138 |
139 | # NCrunch
140 | _NCrunch_*
141 | .*crunch*.local.xml
142 | nCrunchTemp_*
143 |
144 | # MightyMoose
145 | *.mm.*
146 | AutoTest.Net/
147 |
148 | # Web workbench (sass)
149 | .sass-cache/
150 |
151 | # Installshield output folder
152 | [Ee]xpress/
153 |
154 | # DocProject is a documentation generator add-in
155 | DocProject/buildhelp/
156 | DocProject/Help/*.HxT
157 | DocProject/Help/*.HxC
158 | DocProject/Help/*.hhc
159 | DocProject/Help/*.hhk
160 | DocProject/Help/*.hhp
161 | DocProject/Help/Html2
162 | DocProject/Help/html
163 |
164 | # Click-Once directory
165 | publish/
166 |
167 | # Publish Web Output
168 | *.[Pp]ublish.xml
169 | *.azurePubxml
170 | # Note: Comment the next line if you want to checkin your web deploy settings,
171 | # but database connection strings (with potential passwords) will be unencrypted
172 | *.pubxml
173 | *.publishproj
174 |
175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
176 | # checkin your Azure Web App publish settings, but sensitive information contained
177 | # in these scripts will be unencrypted
178 | PublishScripts/
179 |
180 | # NuGet Packages
181 | *.nupkg
182 | # The packages folder can be ignored because of Package Restore
183 | **/[Pp]ackages/*
184 | # except build/, which is used as an MSBuild target.
185 | !**/[Pp]ackages/build/
186 | # Uncomment if necessary however generally it will be regenerated when needed
187 | #!**/[Pp]ackages/repositories.config
188 | # NuGet v3's project.json files produces more ignorable files
189 | *.nuget.props
190 | *.nuget.targets
191 |
192 | # Microsoft Azure Build Output
193 | csx/
194 | *.build.csdef
195 |
196 | # Microsoft Azure Emulator
197 | ecf/
198 | rcf/
199 |
200 | # Windows Store app package directories and files
201 | AppPackages/
202 | BundleArtifacts/
203 | Package.StoreAssociation.xml
204 | _pkginfo.txt
205 | *.appx
206 |
207 | # Visual Studio cache files
208 | # files ending in .cache can be ignored
209 | *.[Cc]ache
210 | # but keep track of directories ending in .cache
211 | !*.[Cc]ache/
212 |
213 | # Others
214 | ClientBin/
215 | ~$*
216 | *~
217 | *.dbmdl
218 | *.dbproj.schemaview
219 | *.jfm
220 | *.pfx
221 | *.publishsettings
222 | orleans.codegen.cs
223 |
224 | # Including strong name files can present a security risk
225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
226 | #*.snk
227 |
228 | # Since there are multiple workflows, uncomment next line to ignore bower_components
229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
230 | #bower_components/
231 |
232 | # RIA/Silverlight projects
233 | Generated_Code/
234 |
235 | # Backup & report files from converting an old project file
236 | # to a newer Visual Studio version. Backup files are not needed,
237 | # because we have git ;-)
238 | _UpgradeReport_Files/
239 | Backup*/
240 | UpgradeLog*.XML
241 | UpgradeLog*.htm
242 | ServiceFabricBackup/
243 | *.rptproj.bak
244 |
245 | # SQL Server files
246 | *.mdf
247 | *.ldf
248 | *.ndf
249 |
250 | # Business Intelligence projects
251 | *.rdl.data
252 | *.bim.layout
253 | *.bim_*.settings
254 | *.rptproj.rsuser
255 |
256 | # Microsoft Fakes
257 | FakesAssemblies/
258 |
259 | # GhostDoc plugin setting file
260 | *.GhostDoc.xml
261 |
262 | # Node.js Tools for Visual Studio
263 | .ntvs_analysis.dat
264 | node_modules/
265 |
266 | # Visual Studio 6 build log
267 | *.plg
268 |
269 | # Visual Studio 6 workspace options file
270 | *.opt
271 |
272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
273 | *.vbw
274 |
275 | # Visual Studio LightSwitch build output
276 | **/*.HTMLClient/GeneratedArtifacts
277 | **/*.DesktopClient/GeneratedArtifacts
278 | **/*.DesktopClient/ModelManifest.xml
279 | **/*.Server/GeneratedArtifacts
280 | **/*.Server/ModelManifest.xml
281 | _Pvt_Extensions
282 |
283 | # Paket dependency manager
284 | .paket/paket.exe
285 | paket-files/
286 |
287 | # FAKE - F# Make
288 | .fake/
289 |
290 | # JetBrains Rider
291 | .idea/
292 | *.sln.iml
293 |
294 | # CodeRush
295 | .cr/
296 |
297 | # Python Tools for Visual Studio (PTVS)
298 | __pycache__/
299 | *.pyc
300 |
301 | # Cake - Uncomment if you are using it
302 | # tools/**
303 | # !tools/packages.config
304 |
305 | # Tabs Studio
306 | *.tss
307 |
308 | # Telerik's JustMock configuration file
309 | *.jmconfig
310 |
311 | # BizTalk build output
312 | *.btp.cs
313 | *.btm.cs
314 | *.odx.cs
315 | *.xsd.cs
316 |
317 | # OpenCover UI analysis results
318 | OpenCover/
319 |
320 | # Azure Stream Analytics local run output
321 | ASALocalRun/
322 |
323 | # MSBuild Binary and Structured Log
324 | *.binlog
325 |
326 | # NVidia Nsight GPU debugger configuration file
327 | *.nvuser
328 |
329 | # MFractors (Xamarin productivity tool) working folder
330 | .mfractor/
331 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Yang Wang
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Sharp Disk Sweeper
2 |
3 | Sharp Disk Sweeper is a very useful tool that helps reveal outstanding huge files/folders that are eating up your disk space the most. The idea is inspired by the popular macOS app [OmniDiskSweeper](https://www.omnigroup.com/more/).
4 |
5 | 
6 |
7 | You can [download it](https://github.com/luanshixia/SharpDiskSweeper/releases) to try it out.
8 |
9 | Sharp Disk Sweeper is written in C# and WPF. The project is a great example for developers who want to learn WPF and XAML, especially on how to customize the look and feel of standard controls using styles and templates. The project also demonstrates how to effectively start/cancel background tasks with `async` and `await` with the UI still responsive.
10 |
11 | Star us, tell your friends, and give us feedback!
12 |
--------------------------------------------------------------------------------
/Screenshot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luanshixia/SharpDiskSweeper/1fbce43ce2306cd2dd1ab21cdde01e3eaac4fbf4/Screenshot.png
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27703.2035
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiskSweeper", "DiskSweeper\DiskSweeper.csproj", "{ACED01E7-FC11-4B41-BEA2-7CEA6720C4A8}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {ACED01E7-FC11-4B41-BEA2-7CEA6720C4A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {ACED01E7-FC11-4B41-BEA2-7CEA6720C4A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {ACED01E7-FC11-4B41-BEA2-7CEA6720C4A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {ACED01E7-FC11-4B41-BEA2-7CEA6720C4A8}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {7C8702F7-7F71-4D6C-A8CC-608C72632DD8}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/App.xaml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System.IO;
2 | using System.Linq;
3 | using System.Windows;
4 |
5 | namespace DiskSweeper
6 | {
7 | ///
8 | /// Interaction logic for App.xaml
9 | ///
10 | public partial class App : Application
11 | {
12 | public static string StartupPath;
13 |
14 | public void Application_Startup(object sender, StartupEventArgs e)
15 | {
16 | if (e.Args.Length > 0 && Directory.Exists(e.Args[0]))
17 | {
18 | App.StartupPath = e.Args[0];
19 | }
20 |
21 | //ShellExtensionAdder.AddRegEntries(ShellExtensionAdder.GetRegEntriesToAdd(
22 | // shellObject: "Directory",
23 | // appName: "SharpDiskSweeper",
24 | // caption: "Open with SharpDiskSweeper",
25 | // command: $"\"{typeof(App).Assembly.Location}\" \"%1\""));
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/ConfigurationHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Configuration;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 | using System.Windows.Media;
8 |
9 | namespace DiskSweeper
10 | {
11 | public static class ConfigurationHelper
12 | {
13 | public static string GetConfiguration(string settingName, string defaultValue = null)
14 | {
15 | return ConfigurationManager.AppSettings.AllKeys.Contains(settingName)
16 | ? ConfigurationManager.AppSettings[settingName]
17 | : defaultValue;
18 | }
19 |
20 | public static long GetConfigurationInt64(string settingName, long defaultValue)
21 | {
22 | return ConfigurationManager.AppSettings.AllKeys.Contains(settingName)
23 | && long.TryParse(ConfigurationManager.AppSettings[settingName], out long result)
24 | ? result
25 | : defaultValue;
26 | }
27 |
28 | public static int GetConfigurationInt32(string settingName, int defaultValue)
29 | {
30 | return ConfigurationManager.AppSettings.AllKeys.Contains(settingName)
31 | && int.TryParse(ConfigurationManager.AppSettings[settingName], out int result)
32 | ? result
33 | : defaultValue;
34 | }
35 |
36 | public static double GetConfigurationDouble(string settingName, double defaultValue)
37 | {
38 | return ConfigurationManager.AppSettings.AllKeys.Contains(settingName)
39 | && double.TryParse(ConfigurationManager.AppSettings[settingName], out double result)
40 | ? result
41 | : defaultValue;
42 | }
43 |
44 | public static TimeSpan GetConfigurationTimeSpan(string settingName, TimeSpan defaultValue)
45 | {
46 | return ConfigurationManager.AppSettings.AllKeys.Contains(settingName)
47 | && TimeSpan.TryParse(ConfigurationManager.AppSettings[settingName], out TimeSpan result)
48 | ? result
49 | : defaultValue;
50 | }
51 |
52 | public static bool GetConfigurationBoolean(string settingName, bool defaultValue)
53 | {
54 | return ConfigurationManager.AppSettings.AllKeys.Contains(settingName)
55 | && bool.TryParse(ConfigurationManager.AppSettings[settingName], out bool result)
56 | ? result
57 | : defaultValue;
58 | }
59 |
60 | public static Guid GetConfigurationGuid(string settingName, Guid defaultValue)
61 | {
62 | return ConfigurationManager.AppSettings.AllKeys.Contains(settingName)
63 | && Guid.TryParse(ConfigurationManager.AppSettings[settingName], out Guid result)
64 | ? result
65 | : defaultValue;
66 | }
67 |
68 | public static Color GetConfigurationColor(string settingName, Color defaultValue)
69 | {
70 | return ConfigurationManager.AppSettings.AllKeys.Contains(settingName)
71 | ? (Color)ColorConverter.ConvertFromString(ConfigurationManager.AppSettings[settingName])
72 | : defaultValue;
73 | }
74 |
75 | public static T GetConfigurationEnum(string settingName, T defaultValue) where T: struct
76 | {
77 | return ConfigurationManager.AppSettings.AllKeys.Contains(settingName)
78 | && Enum.TryParse(ConfigurationManager.AppSettings[settingName], out T result)
79 | ? result
80 | : defaultValue;
81 | }
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/DiskItem.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.ComponentModel;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading;
8 | using System.Threading.Tasks;
9 |
10 | namespace DiskSweeper
11 | {
12 | public class DiskItem : INotifyPropertyChanged
13 | {
14 | public DiskItemType Type { get; set; }
15 | public string Name { get; set; }
16 | public long Size { get; set; }
17 | public long SizeOnDisk { get; set; }
18 | public long FilesCount { get; set; }
19 | public long FoldersCount { get; set; }
20 | public DateTime Created { get; set; }
21 | public DateTime Modified { get; set; }
22 |
23 | public string Highlight => this.Size > SweepEngine.P0SizeFloor
24 | ? "P0"
25 | : this.Size > SweepEngine.P1SizeFloor
26 | ? "P1"
27 | : null;
28 |
29 | public string SizeString => this.IsCalculationDone
30 | ? DiskItem.FormatSize(this.Size)
31 | : "..." + DiskItem.FormatSize(this.Size);
32 |
33 | public string SizeOnDiskString => this.IsCalculationDone
34 | ? DiskItem.FormatSize(this.SizeOnDisk)
35 | : "..." + DiskItem.FormatSize(this.SizeOnDisk);
36 |
37 | public event PropertyChangedEventHandler PropertyChanged;
38 |
39 | private bool IsCalculationDone = false;
40 | private readonly DirectoryInfo DirInfo;
41 |
42 | public DiskItem(FileSystemInfo info)
43 | {
44 | if (info is FileInfo fileInfo)
45 | {
46 | this.Type = DiskItemType.File;
47 | this.Name = fileInfo.Name;
48 | this.Size = fileInfo.Length;
49 | this.SizeOnDisk = fileInfo.GetSizeOnDisk();
50 | this.FilesCount = 1;
51 | this.FoldersCount = 0;
52 | this.Created = fileInfo.CreationTime;
53 | this.Modified = fileInfo.LastWriteTime;
54 | this.IsCalculationDone = true;
55 | }
56 | else if (info is DirectoryInfo directoryInfo)
57 | {
58 | this.Type = DiskItemType.Directory;
59 | this.Name = directoryInfo.Name;
60 | this.Size = 0;
61 | this.SizeOnDisk = 0;
62 | this.FilesCount = 0;
63 | this.FoldersCount = 0;
64 | this.Created = directoryInfo.CreationTime;
65 | this.Modified = directoryInfo.LastWriteTime;
66 | this.DirInfo = directoryInfo;
67 | }
68 | }
69 |
70 | public async Task Start(CancellationToken cancellationToken)
71 | {
72 | if (this.Type == DiskItemType.File)
73 | {
74 | return;
75 | }
76 |
77 | var engine = new SweepEngine(this.DirInfo);
78 | engine.ReportProgress += (sender, e) => this.ReportChanges(engine);
79 |
80 | await Task.Run(() => engine
81 | .CalculateDirectorySizeRecursivelyWithUpdateAsync(this.DirInfo, cancellationToken));
82 |
83 | this.IsCalculationDone = true;
84 | this.ReportChanges(engine);
85 | }
86 |
87 | private void ReportChanges(SweepEngine engine)
88 | {
89 | (this.Size, this.SizeOnDisk, this.FilesCount, this.FoldersCount) = engine.Result;
90 |
91 | this.NotifyPropertyChanged(nameof(this.Size));
92 | this.NotifyPropertyChanged(nameof(this.SizeString));
93 | this.NotifyPropertyChanged(nameof(this.SizeOnDisk));
94 | this.NotifyPropertyChanged(nameof(this.SizeOnDiskString));
95 | this.NotifyPropertyChanged(nameof(this.FilesCount));
96 | this.NotifyPropertyChanged(nameof(this.FoldersCount));
97 | this.NotifyPropertyChanged(nameof(this.Highlight));
98 | }
99 |
100 | private void NotifyPropertyChanged(string propertyName)
101 | {
102 | this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
103 | }
104 |
105 | private static string FormatSize(long size)
106 | {
107 | if (size < 1024)
108 | {
109 | return size + " Byte";
110 | }
111 | else if (size < 1024 * 1024)
112 | {
113 | return (size / 1024) + " KB";
114 | }
115 | else
116 | {
117 | return (size / 1024 / 1024) + " MB";
118 | }
119 | }
120 | }
121 |
122 | public enum DiskItemType
123 | {
124 | File,
125 | Directory
126 | }
127 | }
128 |
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/DiskSweeper.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {ACED01E7-FC11-4B41-BEA2-7CEA6720C4A8}
8 | WinExe
9 | DiskSweeper
10 | DiskSweeper
11 | v4.8
12 | 512
13 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
14 | 4
15 | true
16 |
17 |
18 |
19 | AnyCPU
20 | true
21 | full
22 | false
23 | bin\Debug\
24 | DEBUG;TRACE
25 | prompt
26 | 4
27 |
28 |
29 | AnyCPU
30 | pdbonly
31 | true
32 | bin\Release\
33 | TRACE
34 | prompt
35 | 4
36 |
37 |
38 |
39 | DiskSweeper.ico
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 | 4.0
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 | MSBuild:Compile
61 | Designer
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 | MSBuild:Compile
71 | Designer
72 |
73 |
74 | App.xaml
75 | Code
76 |
77 |
78 | MainWindow.xaml
79 | Code
80 |
81 |
82 |
83 |
84 | Code
85 |
86 |
87 | True
88 | True
89 | Resources.resx
90 |
91 |
92 | True
93 | Settings.settings
94 | True
95 |
96 |
97 | ResXFileCodeGenerator
98 | Resources.Designer.cs
99 |
100 |
101 | SettingsSingleFileGenerator
102 | Settings.Designer.cs
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/DiskSweeper.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luanshixia/SharpDiskSweeper/1fbce43ce2306cd2dd1ab21cdde01e3eaac4fbf4/SharpDiskSweeper/DiskSweeper/DiskSweeper.ico
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/FileInfoExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.ComponentModel;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Runtime.InteropServices;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace DiskSweeper
11 | {
12 | ///
13 | /// https://stackoverflow.com/questions/3750590/get-size-of-file-on-disk
14 | ///
15 | public static class FileInfoExtensions
16 | {
17 | public static long GetSizeOnDisk(this FileInfo info)
18 | {
19 | int result = GetDiskFreeSpaceW(
20 | info.Directory.Root.FullName,
21 | out uint sectorsPerCluster,
22 | out uint bytesPerSector,
23 | out uint dummy,
24 | out dummy);
25 |
26 | if (result == 0)
27 | {
28 | throw new Win32Exception();
29 | }
30 |
31 | uint losize = GetCompressedFileSizeW(
32 | info.FullName,
33 | out uint hosize);
34 |
35 | long size = (long)hosize << 32 | losize;
36 | uint clusterSize = sectorsPerCluster * bytesPerSector;
37 | return ((size + clusterSize - 1) / clusterSize) * clusterSize;
38 | }
39 |
40 | [DllImport("kernel32.dll")]
41 | private static extern uint GetCompressedFileSizeW(
42 | [In, MarshalAs(UnmanagedType.LPWStr)] string lpFileName,
43 | [Out, MarshalAs(UnmanagedType.U4)] out uint lpFileSizeHigh);
44 |
45 | [DllImport("kernel32.dll", SetLastError = true, PreserveSig = true)]
46 | private static extern int GetDiskFreeSpaceW(
47 | [In, MarshalAs(UnmanagedType.LPWStr)] string lpRootPathName,
48 | out uint lpSectorsPerCluster,
49 | out uint lpBytesPerSector,
50 | out uint lpNumberOfFreeClusters,
51 | out uint lpTotalNumberOfClusters);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/GridViewSort.cs:
--------------------------------------------------------------------------------
1 | using System.ComponentModel;
2 | using System.Windows;
3 | using System.Windows.Controls;
4 | using System.Windows.Input;
5 | using System.Windows.Media;
6 |
7 | namespace DiskSweeper
8 | {
9 | ///
10 | /// http://www.thomaslevesque.com/2009/03/27/wpf-automatically-sort-a-gridview-when-a-column-header-is-clicked/
11 | ///
12 | public static class GridViewSort
13 | {
14 | #region Attached properties
15 |
16 | public static ICommand GetCommand(DependencyObject obj)
17 | {
18 | return (ICommand)obj.GetValue(CommandProperty);
19 | }
20 |
21 | public static void SetCommand(DependencyObject obj, ICommand value)
22 | {
23 | obj.SetValue(CommandProperty, value);
24 | }
25 |
26 | // Using a DependencyProperty as the backing store for Command. This enables animation, styling, binding, etc...
27 | public static readonly DependencyProperty CommandProperty =
28 | DependencyProperty.RegisterAttached(
29 | "Command",
30 | typeof(ICommand),
31 | typeof(GridViewSort),
32 | new UIPropertyMetadata(
33 | null,
34 | (d, e) =>
35 | {
36 | if (d is ItemsControl listView)
37 | {
38 | if (!GetAutoSort(listView)) // Don't change click handler if AutoSort enabled
39 | {
40 | if (e.OldValue != null && e.NewValue == null)
41 | {
42 | listView.RemoveHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(ColumnHeader_Click));
43 | }
44 | if (e.OldValue == null && e.NewValue != null)
45 | {
46 | listView.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(ColumnHeader_Click));
47 | }
48 | }
49 | }
50 | }
51 | )
52 | );
53 |
54 | public static bool GetAutoSort(DependencyObject obj)
55 | {
56 | return (bool)obj.GetValue(AutoSortProperty);
57 | }
58 |
59 | public static void SetAutoSort(DependencyObject obj, bool value)
60 | {
61 | obj.SetValue(AutoSortProperty, value);
62 | }
63 |
64 | // Using a DependencyProperty as the backing store for AutoSort. This enables animation, styling, binding, etc...
65 | public static readonly DependencyProperty AutoSortProperty =
66 | DependencyProperty.RegisterAttached(
67 | "AutoSort",
68 | typeof(bool),
69 | typeof(GridViewSort),
70 | new UIPropertyMetadata(
71 | false,
72 | (d, e) =>
73 | {
74 | if (d is ListView listView)
75 | {
76 | if (GetCommand(listView) == null) // Don't change click handler if a command is set
77 | {
78 | bool oldValue = (bool)e.OldValue;
79 | bool newValue = (bool)e.NewValue;
80 | if (oldValue && !newValue)
81 | {
82 | listView.RemoveHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(ColumnHeader_Click));
83 | }
84 | if (!oldValue && newValue)
85 | {
86 | listView.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(ColumnHeader_Click));
87 | }
88 | }
89 | }
90 | }
91 | )
92 | );
93 |
94 | public static string GetPropertyName(DependencyObject obj)
95 | {
96 | return (string)obj.GetValue(PropertyNameProperty);
97 | }
98 |
99 | public static void SetPropertyName(DependencyObject obj, string value)
100 | {
101 | obj.SetValue(PropertyNameProperty, value);
102 | }
103 |
104 | // Using a DependencyProperty as the backing store for PropertyName. This enables animation, styling, binding, etc...
105 | public static readonly DependencyProperty PropertyNameProperty =
106 | DependencyProperty.RegisterAttached(
107 | "PropertyName",
108 | typeof(string),
109 | typeof(GridViewSort),
110 | new UIPropertyMetadata(null)
111 | );
112 |
113 | #endregion
114 |
115 | #region Column header click event handler
116 |
117 | private static void ColumnHeader_Click(object sender, RoutedEventArgs e)
118 | {
119 | if (e.OriginalSource is GridViewColumnHeader headerClicked)
120 | {
121 | string propertyName = GetPropertyName(headerClicked.Column);
122 | if (!string.IsNullOrEmpty(propertyName))
123 | {
124 | ListView listView = GetAncestor(headerClicked);
125 | if (listView != null)
126 | {
127 | ICommand command = GetCommand(listView);
128 | if (command != null)
129 | {
130 | if (command.CanExecute(propertyName))
131 | {
132 | command.Execute(propertyName);
133 | }
134 | }
135 | else if (GetAutoSort(listView))
136 | {
137 | ApplySort(listView.Items, propertyName);
138 | }
139 | }
140 | }
141 | }
142 | }
143 |
144 | #endregion
145 |
146 | #region Helper methods
147 |
148 | public static T GetAncestor(DependencyObject reference) where T : DependencyObject
149 | {
150 | DependencyObject parent = VisualTreeHelper.GetParent(reference);
151 | while (!(parent is T))
152 | {
153 | parent = VisualTreeHelper.GetParent(parent);
154 | }
155 |
156 | return parent as T;
157 | }
158 |
159 | public static void ApplySort(ICollectionView view, string propertyName)
160 | {
161 | ListSortDirection direction = ListSortDirection.Ascending;
162 | if (view.SortDescriptions.Count > 0)
163 | {
164 | SortDescription currentSort = view.SortDescriptions[0];
165 | if (currentSort.PropertyName == propertyName)
166 | {
167 | if (currentSort.Direction == ListSortDirection.Ascending)
168 | {
169 | direction = ListSortDirection.Descending;
170 | }
171 | else
172 | {
173 | direction = ListSortDirection.Ascending;
174 | }
175 | }
176 |
177 | view.SortDescriptions.Clear();
178 | }
179 |
180 | if (!string.IsNullOrEmpty(propertyName))
181 | {
182 | view.SortDescriptions.Add(new SortDescription(propertyName, direction));
183 | }
184 | }
185 |
186 | #endregion
187 | }
188 | }
189 |
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
12 |
53 |
54 |
55 |
56 |
61 |
66 |
71 |
76 |
81 |
86 |
87 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
151 |
152 |
153 |
180 |
181 |
182 |
183 |
184 |
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/MainWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Collections.ObjectModel;
4 | using System.Diagnostics;
5 | using System.IO;
6 | using System.Linq;
7 | using System.Threading;
8 | using System.Threading.Tasks;
9 | using System.Windows;
10 | using System.Windows.Input;
11 | using System.Windows.Media;
12 |
13 | namespace DiskSweeper
14 | {
15 | ///
16 | /// Interaction logic for MainWindow.xaml
17 | ///
18 | public partial class MainWindow : Window
19 | {
20 | private CancellationTokenSource CurrentCancellationTokenSource;
21 | private readonly List History = new List();
22 | private string CurrentPath;
23 | private int HistoryPosition = -1;
24 |
25 | public MainWindow()
26 | {
27 | InitializeComponent();
28 | }
29 |
30 | private async void Window_Initialized(object sender, EventArgs e)
31 | {
32 | this.PathTextBox.Text = App.StartupPath ?? Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
33 | this.UpdateButtonStatus();
34 | await this.NewStart();
35 | }
36 |
37 | private async Task Start()
38 | {
39 | this.CurrentCancellationTokenSource?.Cancel();
40 | this.CurrentCancellationTokenSource = new CancellationTokenSource();
41 | var items = this.GetDiskItems(this.PathTextBox.Text);
42 | this.TheList.ItemsSource = items;
43 | this.TheList.SelectedItem = null;
44 | await Task.WhenAll(items.Select(item => item.Start(this.CurrentCancellationTokenSource.Token)));
45 | }
46 |
47 | private ObservableCollection GetDiskItems(string dirPath)
48 | {
49 | return new ObservableCollection(
50 | collection: new DirectoryInfo(dirPath)
51 | .GetFileSystemInfos()
52 | .Select(info => new DiskItem(info)));
53 | }
54 |
55 | private async Task NewStart(string path = null)
56 | {
57 | if (path != null)
58 | {
59 | this.PathTextBox.Text = path;
60 | }
61 |
62 | if (!DirectoryExists(this.PathTextBox.Text))
63 | {
64 | this.PathTextBox.Foreground = Brushes.Red;
65 | return;
66 | }
67 | else
68 | {
69 | this.PathTextBox.Foreground = Brushes.Black;
70 | this.CurrentPath = this.PathTextBox.Text;
71 | }
72 |
73 | this.HistoryPosition++;
74 | this.History.RemoveRange(this.HistoryPosition, this.History.Count - this.HistoryPosition);
75 | this.History.Add(this.PathTextBox.Text);
76 | this.UpdateButtonStatus();
77 |
78 | await this.Start();
79 | }
80 |
81 | private static bool DirectoryExists(string path)
82 | {
83 | if (!Directory.Exists(path))
84 | {
85 | return false;
86 | }
87 |
88 | try
89 | {
90 | Directory.GetFiles(path);
91 | }
92 | catch (UnauthorizedAccessException)
93 | {
94 | return false;
95 | }
96 |
97 | return true;
98 | }
99 |
100 | private async Task Navigate(int position)
101 | {
102 | this.HistoryPosition = position < 0
103 | ? 0
104 | : position > this.History.Count
105 | ? this.History.Count
106 | : position;
107 |
108 | this.PathTextBox.Text = this.History[this.HistoryPosition];
109 | this.UpdateButtonStatus();
110 |
111 | await this.Start();
112 | }
113 |
114 | private void UpdateButtonStatus()
115 | {
116 | this.BackButton.IsEnabled = this.HistoryPosition > 0;
117 | this.ForwardButton.IsEnabled = this.HistoryPosition < this.History.Count - 1;
118 | this.UpButton.IsEnabled = this.PathTextBox.Text.Length > 3;
119 | }
120 |
121 | private async void StartButton_Click(object sender, RoutedEventArgs e)
122 | {
123 | await this.NewStart();
124 | }
125 |
126 | private async void BackButton_Click(object sender, RoutedEventArgs e)
127 | {
128 | await this.Navigate(this.HistoryPosition - 1);
129 | }
130 |
131 | private async void ForwardButton_Click(object sender, RoutedEventArgs e)
132 | {
133 | await this.Navigate(this.HistoryPosition + 1);
134 | }
135 |
136 | private async void UpButton_Click(object sender, RoutedEventArgs e)
137 | {
138 | await this.NewStart(path: Path.GetDirectoryName(this.PathTextBox.Text));
139 | }
140 |
141 | private async void PathTextBox_KeyUp(object sender, KeyEventArgs e)
142 | {
143 | if (e.Key == Key.Enter)
144 | {
145 | await this.NewStart();
146 | }
147 | }
148 |
149 | private async void TheList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
150 | {
151 | if (this.TheList.SelectedItem is DiskItem item)
152 | {
153 | if (item.Type != DiskItemType.Directory)
154 | {
155 | return;
156 | }
157 |
158 | await this.NewStart(path: Path.Combine(this.CurrentPath, item.Name));
159 | }
160 | }
161 |
162 | private void ExploreButton_Click(object sender, RoutedEventArgs e)
163 | {
164 | Process.Start("explorer", this.PathTextBox.Text);
165 | }
166 | }
167 | }
168 |
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Resources;
3 | using System.Runtime.CompilerServices;
4 | using System.Runtime.InteropServices;
5 | using System.Windows;
6 |
7 | // General Information about an assembly is controlled through the following
8 | // set of attributes. Change these attribute values to modify the information
9 | // associated with an assembly.
10 | [assembly: AssemblyTitle("DiskSweeper")]
11 | [assembly: AssemblyDescription("")]
12 | [assembly: AssemblyConfiguration("")]
13 | [assembly: AssemblyCompany("Yang Wang (GitHub: @luanshixia)")]
14 | [assembly: AssemblyProduct("Disk Sweeper")]
15 | [assembly: AssemblyCopyright("Copyright © Yang Wang 2018")]
16 | [assembly: AssemblyTrademark("")]
17 | [assembly: AssemblyCulture("")]
18 |
19 | // Setting ComVisible to false makes the types in this assembly not visible
20 | // to COM components. If you need to access a type in this assembly from
21 | // COM, set the ComVisible attribute to true on that type.
22 | [assembly: ComVisible(false)]
23 |
24 | //In order to begin building localizable applications, set
25 | //CultureYouAreCodingWith in your .csproj file
26 | //inside a . For example, if you are using US english
27 | //in your source files, set the to en-US. Then uncomment
28 | //the NeutralResourceLanguage attribute below. Update the "en-US" in
29 | //the line below to match the UICulture setting in the project file.
30 |
31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
32 |
33 |
34 | [assembly: ThemeInfo(
35 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
36 | //(used if a resource is not found in the page,
37 | // or application resource dictionaries)
38 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
39 | //(used if a resource is not found in the page,
40 | // app, or any theme specific resource dictionaries)
41 | )]
42 |
43 |
44 | // Version information for an assembly consists of the following four values:
45 | //
46 | // Major Version
47 | // Minor Version
48 | // Build Number
49 | // Revision
50 | //
51 | // You can specify all the values or you can default the Build and Revision Numbers
52 | // by using the '*' as shown below:
53 | // [assembly: AssemblyVersion("1.0.*")]
54 | [assembly: AssemblyVersion("1.0.0.0")]
55 | [assembly: AssemblyFileVersion("1.0.0.0")]
56 |
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/Properties/Resources.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace DiskSweeper.Properties {
12 | using System;
13 |
14 |
15 | ///
16 | /// A strongly-typed resource class, for looking up localized strings, etc.
17 | ///
18 | // This class was auto-generated by the StronglyTypedResourceBuilder
19 | // class via a tool like ResGen or Visual Studio.
20 | // To add or remove a member, edit your .ResX file then rerun ResGen
21 | // with the /str option, or rebuild your VS project.
22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
25 | internal class Resources {
26 |
27 | private static global::System.Resources.ResourceManager resourceMan;
28 |
29 | private static global::System.Globalization.CultureInfo resourceCulture;
30 |
31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
32 | internal Resources() {
33 | }
34 |
35 | ///
36 | /// Returns the cached ResourceManager instance used by this class.
37 | ///
38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
39 | internal static global::System.Resources.ResourceManager ResourceManager {
40 | get {
41 | if (object.ReferenceEquals(resourceMan, null)) {
42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("DiskSweeper.Properties.Resources", typeof(Resources).Assembly);
43 | resourceMan = temp;
44 | }
45 | return resourceMan;
46 | }
47 | }
48 |
49 | ///
50 | /// Overrides the current thread's CurrentUICulture property for all
51 | /// resource lookups using this strongly typed resource class.
52 | ///
53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
54 | internal static global::System.Globalization.CultureInfo Culture {
55 | get {
56 | return resourceCulture;
57 | }
58 | set {
59 | resourceCulture = value;
60 | }
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/Properties/Resources.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
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 | text/microsoft-resx
107 |
108 |
109 | 2.0
110 |
111 |
112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
113 |
114 |
115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace DiskSweeper.Properties {
12 |
13 |
14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.3.0.0")]
16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
17 |
18 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
19 |
20 | public static Settings Default {
21 | get {
22 | return defaultInstance;
23 | }
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/Resources/File.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luanshixia/SharpDiskSweeper/1fbce43ce2306cd2dd1ab21cdde01e3eaac4fbf4/SharpDiskSweeper/DiskSweeper/Resources/File.png
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/Resources/Folder.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luanshixia/SharpDiskSweeper/1fbce43ce2306cd2dd1ab21cdde01e3eaac4fbf4/SharpDiskSweeper/DiskSweeper/Resources/Folder.png
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/ShellExtensionAdder.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Diagnostics;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace DiskSweeper
9 | {
10 | public class ShellExtensionAdder
11 | {
12 | //private readonly Dictionary RegValuesToAdd = new Dictionary
13 | //{
14 | // { @"HKEY_CLASSES_ROOT\Directory\shell\SharpDiskSweeper", "@=Open with SharpDiskSweeper&Icon=" },
15 | // { @"HKEY_CLASSES_ROOT\Directory\shell\SharpDiskSweeper\command", "@=\"\" \"%1\"" }
16 | //};
17 |
18 | //public static Dictionary GetRegValuesToAdd(string shellObject, string appName, string caption, string command, string icon) => new Dictionary
19 | //{
20 | // { $@"HKEY_CLASSES_ROOT\{shellObject}\shell\{appName}", $"@={caption}&Icon={icon}" },
21 | // { $@"HKEY_CLASSES_ROOT\{shellObject}\shell\{appName}\command", $"@={command}" }
22 | //};
23 |
24 | public static (string, string, string, string)[] GetRegEntriesToAdd(string shellObject, string appName, string caption, string command, string icon = null) => new[]
25 | {
26 | ($@"HKEY_CLASSES_ROOT\{shellObject}\shell\{appName}", "", caption, "REG_EXPAND_SZ"),
27 | ($@"HKEY_CLASSES_ROOT\{shellObject}\shell\{appName}", "Icon", icon, "REG_EXPAND_SZ"),
28 | ($@"HKEY_CLASSES_ROOT\{shellObject}\shell\{appName}\command", "", command, "REG_EXPAND_SZ")
29 | }.Where(entry => entry.Item3 != null).ToArray();
30 |
31 | public static void AddRegEntries((string, string, string, string)[] regValues)
32 | {
33 | foreach (var regValue in regValues)
34 | {
35 | AddReg(regValue.Item1, regValue.Item2, regValue.Item3, regValue.Item4);
36 | }
37 | }
38 |
39 | public static void AddReg(string key, string valueName, string data, string dataType = "REG_SZ", string separator = null, bool force = true)
40 | {
41 | Process.Start("reg", $"add {key}"
42 | + (!string.IsNullOrWhiteSpace(valueName) ? $" /v {valueName}" : "")
43 | + $" /t {dataType}"
44 | + (separator != null ? $" /s {separator}" : "")
45 | + $" /d '{data}'"
46 | + (force ? " /f" : ""));
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/SharpDiskSweeper/DiskSweeper/SweepEngine.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Diagnostics;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading;
8 | using System.Threading.Tasks;
9 |
10 | namespace DiskSweeper
11 | {
12 | public class SweepEngine
13 | {
14 | private static readonly Dictionary ResultCache = new Dictionary();
15 |
16 | private readonly DirectoryInfo Directory;
17 | private const int ProgressReportInterval = 200;
18 |
19 | private long TotalSize;
20 | private long TotalSizeOnDisk;
21 | private long TotalFilesCount;
22 | private long TotalFoldersCount;
23 |
24 | private long ChangesCount;
25 |
26 | public event EventHandler ReportProgress;
27 |
28 | public (long, long, long, long) Result => (this.TotalSize, this.TotalSizeOnDisk, this.TotalFilesCount, this.TotalFoldersCount);
29 |
30 | public static long P0SizeFloor => ConfigurationHelper.GetConfigurationInt64(
31 | settingName: "DiskSweeper.Highlights.P0.SizeFloor",
32 | defaultValue: 1073741824L);
33 |
34 | public static long P1SizeFloor => ConfigurationHelper.GetConfigurationInt64(
35 | settingName: "DiskSweeper.Highlights.P1.SizeFloor",
36 | defaultValue: 134217728L);
37 |
38 | public SweepEngine(DirectoryInfo dir)
39 | {
40 | this.Directory = dir;
41 | }
42 |
43 | public async Task CalculateDirectorySizeRecursivelyWithUpdateAsync(DirectoryInfo directory, CancellationToken cancellationToken)
44 | {
45 | try
46 | {
47 | foreach (var childDirectory in directory.GetDirectories())
48 | {
49 | await this.CalculateDirectorySizeRecursivelyWithUpdateAsync(childDirectory, cancellationToken);
50 |
51 | if (cancellationToken.IsCancellationRequested)
52 | {
53 | break;
54 | }
55 | }
56 |
57 | var files = directory.GetFiles();
58 | this.TotalSize += files.Sum(file => file.Length);
59 | //this.TotalSizeOnDisk += files.Sum(file => file.GetSizeOnDisk());
60 | this.TotalFilesCount += files.Count();
61 | this.TotalFoldersCount += 1;
62 |
63 | this.CountChanges();
64 | }
65 | catch (UnauthorizedAccessException ex)
66 | {
67 | Trace.WriteLine(ex.Message);
68 | }
69 | catch (DirectoryNotFoundException ex)
70 | {
71 | Trace.WriteLine(ex.Message);
72 | }
73 | }
74 |
75 | private async Task<(long, long, long, long)> GetDirectorySizeRecursivelyAsync(DirectoryInfo directory, CancellationToken cancellationToken)
76 | {
77 | try
78 | {
79 | var childDirResults = await Task.WhenAll(directory.GetDirectories()
80 | .Select(childDirectory => this.GetDirectorySizeRecursivelyAsync(childDirectory, cancellationToken)));
81 |
82 | var files = directory.GetFiles();
83 |
84 | var result = (
85 | childDirResults.Sum(r => r.Item1) + files.Sum(file => file.Length),
86 | childDirResults.Sum(r => r.Item2),
87 | childDirResults.Sum(r => r.Item3) + files.Length,
88 | childDirResults.Sum(r => r.Item4) + 1);
89 |
90 | SweepEngine.ResultCache[directory.FullName] = result;
91 |
92 | return result;
93 | }
94 | catch (UnauthorizedAccessException ex)
95 | {
96 | Trace.WriteLine(ex.Message);
97 | }
98 | catch (DirectoryNotFoundException ex)
99 | {
100 | Trace.WriteLine(ex.Message);
101 | }
102 |
103 | return (0, 0, 0, 0);
104 | }
105 |
106 | private void CountChanges()
107 | {
108 | this.ChangesCount++;
109 | if (this.ChangesCount % SweepEngine.ProgressReportInterval == 0)
110 | {
111 | this.ReportProgress?.Invoke(this, null);
112 | }
113 | }
114 | }
115 | }
116 |
--------------------------------------------------------------------------------