-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainMenu.cs
More file actions
369 lines (327 loc) · 12.3 KB
/
MainMenu.cs
File metadata and controls
369 lines (327 loc) · 12.3 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace DeleterApp
{
public partial class MainMenu : Form
{
private Timer timer;
private Timer Timer_Revert;
private Size startSize = new Size(426, 315);
private Size targetSize = new Size(900, 600);
private const int stepSize = 10;
private const int timerInterval = 20;
private const int timerInterval_revert = 20;
private List<string> fileList = new List<string>();
private int fileCount = 0, folderCount = 0;
private long totalFileWeight = 0, totalFolderWeight = 0;
public MainMenu()
{
InitializeComponent();
this.ClientSize = startSize;
timer = new Timer();
Timer_Revert = new Timer();
Timer_Revert.Interval = timerInterval_revert;
timer.Interval = timerInterval;
timer.Tick += Timer_Tick;
Timer_Revert.Tick += Timer_Tick_Revert;
}
private void Timer_Tick(object sender, EventArgs e)
{
if (this.ClientSize.Width < targetSize.Width || this.ClientSize.Height < targetSize.Height)
{
this.ClientSize = new Size(ClientSize.Width + stepSize, this.ClientSize.Height + stepSize);
CenterToScreen();
}
else
{
timer.Stop();
label3.Visible = true;
label4.Visible = true;
label5.Visible = true;
label6.Visible = true;
ClearAllButton.Visible = true;
ClearAllFull.Visible = true;
ClearAllDefault.Visible = true;
progressBar1.Visible = true;
}
}
private void Timer_Tick_Revert(object sender, EventArgs e)
{
if (this.ClientSize.Width > startSize.Width || this.ClientSize.Height > startSize.Height)
{
this.ClientSize = new Size(ClientSize.Width - stepSize, this.ClientSize.Height - stepSize);
CenterToScreen();
}
else
{
Timer_Revert.Stop();
}
}
void panel1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop) == true)
{
label1.Text = "Отпустите мышь";
e.Effect = DragDropEffects.Copy;
}
}
void panel1_DragLeave(object sender, EventArgs e)
{
label1.Text = "Перетащите файл сюда";
}
void panel1_DragDrop(object sender, DragEventArgs e)
{
label1.Text = "Перетащите файл сюда";
timer.Start();
LoadingGif.Visible = true;
Cursor.Current = Cursors.WaitCursor;
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files != null && files.Length > 0)
{
StringBuilder fileOutput = new StringBuilder();
foreach (string path in files)
{
if (File.Exists(path))
{
if (!fileList.Contains(path))
{
fileList.Add(path);
FileInfo fileInfo = new FileInfo(path);
fileOutput.AppendLine($"{path} (файл)");
fileCount++;
totalFileWeight += fileInfo.Length;
}
}
else if (Directory.Exists(path))
{
if (!fileList.Contains(path))
{
fileList.Add(path);
DirectoryInfo dirInfo = new DirectoryInfo(path);
fileOutput.AppendLine($"{path} (папка)");
folderCount++;
totalFolderWeight += GetDirectoryWeight(dirInfo, ref fileCount, ref folderCount, ref totalFileWeight, ref totalFolderWeight);
}
}
}
string output = fileOutput.ToString();
label3.Text += Environment.NewLine + output;
UpdateCountLabels();
UpdateTotalWeightLabels();
}
Cursor.Current = Cursors.Arrow;
LoadingGif.Visible = false;
}
private void UpdateCountLabels()
{
label4.Text = $"Файлы: {fileCount}";
label5.Text = $"Папки: {folderCount}";
}
private void UpdateTotalWeightLabels()
{
label6.Text = $"Весь вес папок {(totalFolderWeight / 1024.0 / 1024.0):F2} MB\nВесь вес файлов: {(totalFileWeight / 1024.0 / 1024.0):F2} MB";
}
private long GetDirectoryWeight(DirectoryInfo dirInfo, ref int fileCount, ref int folderCount, ref long totalFileWeight, ref long totalFolderWeight)
{
long weight = 0;
FileInfo[] fileInfos = dirInfo.GetFiles();
foreach (FileInfo fileInfo in fileInfos)
{
weight += fileInfo.Length;
fileCount++;
totalFileWeight += fileInfo.Length;
}
DirectoryInfo[] subDirs = dirInfo.GetDirectories();
foreach (DirectoryInfo subDir in subDirs)
{
weight += GetDirectoryWeight(subDir, ref fileCount, ref folderCount, ref totalFileWeight, ref totalFolderWeight);
folderCount++;
totalFolderWeight += weight;
}
return weight;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
float[] dashes = { 3, 1 };
Pen pen = new Pen(Color.Black, 2);
pen.DashPattern = dashes;
e.Graphics.DrawRectangle(pen, 1, 1, panel1.Width - 2, panel1.Height - 2);
Update();
}
private void button1_Click(object sender, EventArgs e)
{
CleanListFiles();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private bool Warning()
{
DialogResult dialogResult = MessageBox.Show("Вы точно хотите удалить эти файлы?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
switch (dialogResult)
{
case DialogResult.Yes:
return true;
case DialogResult.No:
return false;
default:
return false;
}
}
private void Minimize_MouseEnter(object sender, EventArgs e)
{
MinimizeForm.BackColor = Color.FromArgb(64, 64, 64);
}
private void Close_MouseEnter(object sender, EventArgs e)
{
CloseForm.BackColor = Color.Maroon;
}
private void Close_MouseLeave(object sender, EventArgs e)
{
CloseForm.BackColor = Color.Transparent;
}
private void Minimize_MouseLeave(object sender, EventArgs e)
{
MinimizeForm.BackColor = Color.Transparent;
}
private void Close_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void LoadingGif_Paint(object sender, PaintEventArgs e)
{
}
private void panel1_Click(object sender, EventArgs e)
{
}
private Point LastPos;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
var control = this.GetChildAtPoint(e.Location);
if (control != null && control != this && !typeof(Label).IsAssignableFrom(control.GetType()))
{
return;
}
this.Left += e.X - LastPos.X;
this.Top += e.Y - LastPos.Y;
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
LastPos = e.Location;
}
private void ClearAllDefault_Click(object sender, EventArgs e)
{
bool result = Warning();
if (result == false)
{
MessageBox.Show("Действие отменено", "Deleter", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
Cursor.Current = Cursors.WaitCursor;
progressBar1.Value += 10;
foreach (string path in fileList)
{
try
{
if (File.Exists(path))
{
File.Delete(path);
}
else if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка удаления, не все файлы были удалены, код: {ex}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
progressBar1.Value += 10;
Cursor.Current = Cursors.Default;
MessageBox.Show("Операция удаления прошла успешно", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
CleanListFiles();
}
private void ClearAllFull_Click(object sender, EventArgs e)
{
bool result = Warning();
if (result == false)
{
MessageBox.Show("Действие отменено", "Deleter", MessageBoxButtons.OK);
return;
}
Cursor.Current = Cursors.WaitCursor;
progressBar1.Value += 10;
foreach (string path in fileList)
{
fullDelete(path);
}
progressBar1.Value += 10;
Cursor.Current = Cursors.Default;
MessageBox.Show("Операция удаления прошла успешно", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
CleanListFiles();
}
void fullDelete(string path)
{
try
{
if (File.Exists(path))
{
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Write))
{
fs.SetLength(0);
}
File.Delete(path);
}
else if (Directory.Exists(path))
{
foreach (string files in Directory.GetFiles(path))
{
fullDelete(files);
}
foreach (string dirs in Directory.GetDirectories(path))
{
fullDelete(dirs);
}
Directory.Delete(path, true);
}
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка удаления, не все файлы были удалены, код: {ex}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Minimize_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
}
void CleanListFiles()
{
fileList.Clear();
fileCount = 0;
folderCount = 0;
totalFileWeight = 0;
totalFolderWeight = 0;
label3.Text = "";
UpdateCountLabels();
UpdateTotalWeightLabels();
Timer_Revert.Start();
label3.Visible = false;
label4.Visible = false;
label5.Visible = false;
label6.Visible = false;
ClearAllButton.Visible = false;
ClearAllFull.Visible = false;
ClearAllDefault.Visible = false;
progressBar1.Value = progressBar1.Minimum;
progressBar1.Visible = false;
}
}
}