-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
238 lines (224 loc) · 8.05 KB
/
Form1.cs
File metadata and controls
238 lines (224 loc) · 8.05 KB
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
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace DirectorySize
{
public partial class Form1 : Form
{
DirectorySizeCalculator calc = null;
public Form1()
{
InitializeComponent();
}
private void btnChoseFolder_Click(object sender, EventArgs e)
{
var result = dlgFolderBrowser.ShowDialog();
if (result == DialogResult.OK || result == DialogResult.Yes)
{
txtFolderPath.Text = dlgFolderBrowser.SelectedPath;
}
}
private void btnAnalyzer_Click(object sender, EventArgs e)
{
calc = new DirectorySizeCalculator(txtFolderPath.Text.Trim(), onSizeUpdated);
calc.Calculate();
}
private void onSizeUpdated(SortedDictionary<string, long> sizes, bool isOk)
{
if (!isOk)
{
this.Invoke(new MethodInvoker(delegate ()
{
btnAnalyzer.Enabled = false;
btnChoseFolder.Enabled = false;
btnStop.Enabled = true;
label1.ForeColor = Color.DarkBlue;
label1.Text = "running...";
List<string> lines = new List<string>();
try
{
foreach (var sz in sizes.OrderByDescending(s => s.Value).Take(100))
{
lines.Add(string.Format("{0}: {1}", sz.Key, GetFileSize(sz.Value)));
}
}
catch (Exception ex)
{
lines.Clear();
}
if (lines.Count > 0)
{
lblStats.Text = sizes.Count.ToString("#,#") + " directories";
textBox1.Clear();
textBox1.Lines = lines.ToArray();
}
}));
}
else
{
this.Invoke(new MethodInvoker(delegate ()
{
btnAnalyzer.Enabled = true;
btnChoseFolder.Enabled = true;
btnStop.Enabled = false;
label1.ForeColor = Color.Red;
label1.Text = "Finished!";
}
));
}
}
private string GetFileSize(long byteCount)
{
string size = "0 Bytes";
if (byteCount >= 1073741824.0)
size = String.Format("{0:##.##}", byteCount / 1073741824.0) + " GB";
else if (byteCount >= 1048576.0)
size = String.Format("{0:##.##}", byteCount / 1048576.0) + " MB";
else if (byteCount >= 1024.0)
size = String.Format("{0:##.##}", byteCount / 1024.0) + " KB";
else if (byteCount > 0 && byteCount < 1024.0)
size = byteCount.ToString() + " Bytes";
return size;
}
private void btnStop_Click(object sender, EventArgs e)
{
calc.Stop();
btnAnalyzer.Enabled = true;
btnChoseFolder.Enabled = true;
btnStop.Enabled = false;
}
}
public class DirectorySizeCalculator
{
string _rootPath = "";
DirectoryInfo _rootFolder;
SortedDictionary<string, long> _sizes;
object _locker = new object();
Action<SortedDictionary<string, long>, bool> _onSizeUpdated;
bool _isOk = false;
public DirectorySizeCalculator(string rootPath, Action<SortedDictionary<string, long>, bool> onSizeUpdated = null)
{
_rootPath = rootPath;
_rootFolder = new DirectoryInfo(_rootPath);
_sizes = new SortedDictionary<string, long>();
_onSizeUpdated = onSizeUpdated;
}
List<Thread> _tasks = new List<Thread>();
public void Stop()
{
_tasks.ForEach(th => th.Abort());
_tasks.Clear();
_sizes.Clear();
_isOk = false;
}
public void Calculate()
{
_tasks.ForEach(th => th.Abort());
_tasks.Clear();
_sizes.Clear();
_isOk = false;
if (_onSizeUpdated != null)
{
var thrSizeUpdated = new Thread(() =>
{
Thread.Sleep(500);
while (!_isOk)
{
_onSizeUpdated.Invoke(_sizes, _isOk);
Thread.Sleep(500);
}
_onSizeUpdated.Invoke(_sizes, _isOk);
});
thrSizeUpdated.Start();
_tasks.Add(thrSizeUpdated);
}
if (_rootFolder.Exists)
{
ConcurrentQueue<FileInfo> files = new ConcurrentQueue<FileInfo>();
var thrCrawlFiles = new Thread(() =>
{
Thread.Sleep(100);
CrawlFiles(files, _rootFolder, "*.*");
_isOk = true;
});
thrCrawlFiles.Start();
_tasks.Add(thrCrawlFiles);
var thrAddFileSize = new Thread(() =>
{
while (!_isOk)
{
FileInfo file;
if (files == null || files.Count == 0)
{
Thread.Sleep(100);
}
else
{
if (files.TryDequeue(out file))
{
addFileSize(file, null);
}
}
}
});
thrAddFileSize.Start();
_tasks.Add(thrAddFileSize);
}
}
IEnumerable<FileInfo> EnumerateFiles(DirectoryInfo path, string searchPattern)
{
return path.EnumerateFiles(searchPattern).Union(path.EnumerateDirectories().SelectMany(
d => { try { return EnumerateFiles(d, searchPattern); } catch { return new List<FileInfo>(); } }));
}
void CrawlFiles(ConcurrentQueue<FileInfo> files, DirectoryInfo path, string searchPattern)
{
foreach (var f in path.EnumerateFiles(searchPattern))
{
files.Enqueue(f);
}
foreach (var d in path.EnumerateDirectories())
{
try { CrawlFiles(files, d, searchPattern); }
catch
{
}
}
}
private void addFileSize(FileInfo file, DirectoryInfo directory)
{
var folder = directory == null ? file.Directory : directory;
if (folder != null)
{
if (!_sizes.ContainsKey(folder.FullName))
{
lock (_locker)
{
_sizes.Add(folder.FullName, file.Length);
}
}
else
{
lock (_locker)
{
long size = _sizes[folder.FullName];
_sizes[folder.FullName] = size + file.Length;
}
}
if (!folder.FullName.Equals(_rootFolder.FullName))
{
addFileSize(file, folder.Parent);
}
}
}
}
}