-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeSizeForm.cs
235 lines (206 loc) · 7.85 KB
/
TreeSizeForm.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
using DevExpress.XtraTreeList.Nodes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TreeSizeApp
{
public partial class TreeSizeForm : Form
{
private readonly long BIGTEXTSIZE = 1024 * 1024 * 100;
private readonly HashSet<string> textExtNameSet = new HashSet<string>(StringComparer.CurrentCultureIgnoreCase) {
".txt", ".log", ".xml", ".dat", ".fil", ".eod", ".cdl", ".cdr"
};
public TreeSizeForm()
{
InitializeComponent();
}
private void btnSearch_Click(object sender, EventArgs e)
{
string path = txtPath.Text.Trim();
if (string.IsNullOrWhiteSpace(path))
{
MessageBox.Show("Path cannot be empty.");
return;
}
if (!Directory.Exists(path))
{
MessageBox.Show("Don't exist this folder: " + path);
return;
}
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(string));
table.Columns.Add("ParentID", typeof(string));
table.Columns.Add("FileName", typeof(string));
table.Columns.Add("LastUpdate", typeof(DateTime));
table.Columns.Add("FileSize", typeof(long));
table.Columns.Add("FileSize_MB", typeof(long), "FileSize/1024/1024");
table.Columns.Add("TextSize", typeof(long));
table.PrimaryKey = new DataColumn[] { table.Columns["ID"] };
txtComment.Text = "---";
txtSize.Text = "Checking...";
string parentId = "";
Tuple<long, long> fileSize = Lookup(parentId, path, table);
txtSize.Text = string.Format("TextSize:{0}M TotalSize:{1}M",
(fileSize.Item2/1024/1024).ToString("###,##0"),
(fileSize.Item1/1024/1024).ToString("###,##0"));
treeList.DataSource = table;
}
private Tuple<long, long> Lookup(string parentId, string path, DataTable table)
{
int index = 0;
long totalSize = 0;
long textTotalSize = 0;
try
{
String[] dirs = Directory.GetDirectories(path);
foreach (string dir in dirs)
{
index++;
string id = string.Format("{0}_{1}", parentId, index);
DirectoryInfo di = new DirectoryInfo(path);
DataRow row = AddRow(id, parentId, table, dir, di.LastWriteTime, -1, -1);
Tuple<long, long> temp = Lookup(id, dir, table);
row["FileSize"] = temp.Item1;
row["TextSize"] = temp.Item2;
totalSize += temp.Item1;
textTotalSize += temp.Item2;
index++;
}
Tuple<long, long> tempFileSize = AddFileIntoTree(index, parentId, path, table);
totalSize += tempFileSize.Item1;
textTotalSize += tempFileSize.Item2;
}
catch (Exception ex)
{
AddComment(string.Format("{0}:{1}", path, ex.Message), true);
}
return new Tuple<long, long>(totalSize, textTotalSize);
}
private Tuple<long, long> AddFileIntoTree(int maxIndex, string parentId, string path, DataTable table)
{
long totalSize = 0;
long textTotalSize = 0;
StringBuilder sb = new StringBuilder();
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] fileInfos = di.GetFiles();
foreach (FileInfo fileInfo in fileInfos)
{
string id = string.Format("{0}_{1}", parentId, maxIndex);
string fileName = Path.GetFileName(fileInfo.FullName);
long fileSize = fileInfo.Length;
bool isTextFile = IsTextFile(fileInfo);
AddRow(id, parentId, table, fileName, fileInfo.LastWriteTime, fileSize, isTextFile ? fileSize : 0);
totalSize += fileSize;
if (isTextFile)
{
textTotalSize += fileSize;
}
maxIndex++;
}
return new Tuple<long, long>(totalSize, textTotalSize);
}
private DataRow AddRow(string Id, string parentId, DataTable table, string fileName, DateTime lastUpdate, long fileSize, long textSize)
{
DataRow row = table.NewRow();
row["ID"] = Id;
row["ParentID"] = parentId;
row["FileName"] = fileName;
row["LastUpdate"] = lastUpdate;
row["FileSize"] = fileSize;
row["TextSize"] = textSize;
table.Rows.Add(row);
return row;
}
private bool IsTextFile(FileInfo fileInfo)
{
// Currently only check its extension name
string extName = fileInfo.Extension;
return textExtNameSet.Contains(extName);
}
private void AddComment(string comment, bool append = true)
{
if (!string.IsNullOrEmpty(comment))
{
if (append)
{
txtComment.Text += comment + "\r\n";
}
else
{
txtComment.Text = comment;
}
}
}
private void treeList_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
{
if (e?.Column?.FieldName == "TextSize")
{
if (e.CellValue != null)
{
long temp = Convert.ToInt64(e.CellValue);
if (temp > 0)
{
e.Appearance.BackColor = Color.LightYellow;
if (temp > BIGTEXTSIZE)
{
e.Appearance.ForeColor = Color.Red;
}
}
}
}
}
private void btnOpenFolder_Click(object sender, EventArgs e)
{
var focusedNode = treeList.FocusedNode;
if (focusedNode != null)
{
string path = "";
if (focusedNode.HasChildren)
{
path = focusedNode.GetValue(colFileName) as string;
}
else
{
TreeListNode parentNode = focusedNode.ParentNode;
path = Path.Combine(parentNode.GetValue(colFileName) as string, focusedNode.GetValue(colFileName) as string);
}
if (!string.IsNullOrWhiteSpace(path))
{
System.Diagnostics.Process.Start("explorer", string.Format("/select, \"{0}\"",path));
}
}
}
private void btnZipFolder_Click(object sender, EventArgs e)
{
string path = "";
var focusedNode = treeList.FocusedNode;
if (focusedNode != null)
{
if (focusedNode.HasChildren)
{
path = focusedNode.GetValue(colFileName) as string;
}
}
if (string.IsNullOrWhiteSpace(path))
{
MsgHelper.ShowMessage("Please choose a folder.");
return;
}
if (ZipHelper.ZipFolder(path, chkRemoveZippedFolder.Checked))
{
MsgHelper.ShowMessage("Zipped successfully.");
}
else
{
MsgHelper.ShowMessage("Failed to zip.");
}
}
}
}