-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCleanerForm.cs
More file actions
401 lines (333 loc) · 13.1 KB
/
CleanerForm.cs
File metadata and controls
401 lines (333 loc) · 13.1 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Disk_Scraper
{
public partial class CleanerForm : Form
{
// Import the necessary Windows API functions
[DllImport("kernel32.dll")]
static extern bool FreeConsole();
// Import the AllocConsole function from kernel32.dll
[DllImport("kernel32.dll")]
static extern bool AllocConsole();
public CleanerForm()
{
InitializeComponent();
}
private void StartCleanBtn_Click(object sender, EventArgs e)
{
AllocConsole();
CleanTempFiles();
}
public void CleanTempFiles()
{
try
{
// Get the path to the system temporary folder
string systemTempFolder = Path.GetTempPath();
string userTempFolder = Environment.GetEnvironmentVariable("TEMP");
string prefetchFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Prefetch");
string downloadsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads");
string CustomDirFolder = CustomDirectory.Text;
string CustomDirFolder2 = CustomDirectory2.Text;
string CustomDirFolder3 = CustomDirectory3.Text;
/* yesterday i add Vs Files,
Temp Files,
Prefetch Files,
Download Files ,
and CustomDir Files
Deletion
*/
// Add Log File deleter today
CleanerProgressBar.Value = 10;
// Clean System Temp Folder
if (TempFilesCheckbox.Checked)
{
// Cleans System Temp Folder
CleanFolder(systemTempFolder);
CleanerProgressBar.Value = 20;
// Clean User Temp Folder
CleanFolder(userTempFolder);
CleanerProgressBar.Value = 30;
}
if (PrefetchCheckBox.Checked)
{
// Clean Prefetch Folder
CleanFolder(prefetchFolder);
CleanerProgressBar.Value = 50;
}
if (DownloadsCheckBox.Checked)
{
CleanFolder(downloadsPath);
CleanerProgressBar.Value = 60;
}
if (CustomPathCheckBox.Checked)
{
if (Directory.Exists(CustomDirFolder))
{
CleanFolder(CustomDirFolder);
}
else
{
MessageBox.Show("Directory does not exist !");
}
CleanerProgressBar.Value = 65;
}
if (CustomPathCheckBox2.Checked)
{
if (Directory.Exists(CustomDirFolder2))
{
CleanFolder(CustomDirFolder2);
}
else
{
MessageBox.Show("Directory does not exist !");
}
CleanerProgressBar.Value = 70;
}
if (CustomPathCheckBox3.Checked)
{
if (Directory.Exists(CustomDirFolder3))
{
CleanFolder(CustomDirFolder3);
}
else
{
MessageBox.Show("Directory does not exist !");
}
CleanerProgressBar.Value = 75;
}
if (VsFilesCheckBox.Checked)
{
DeleteVsFiles();
CleanerProgressBar.Value = 80;
}
if(LogFilesCheckBox.Checked)
{
DeleteLogAndTraceFilesFromAllDrives();
}
CleanerProgressBar.Value = 100;
Console.WriteLine("Temporary files cleanup completed.");
// Open Folders
if(OpenDirsCheckBox.Checked)
{
if (TempFilesCheckbox.Checked)
{
OpenDirectory(systemTempFolder);
OpenDirectory(userTempFolder);
}
if (PrefetchCheckBox.Checked)
{
OpenDirectory(prefetchFolder);
}
if (CustomPathCheckBox.Checked)
{
OpenDirectory(CustomDirFolder);
}
if (CustomPathCheckBox2.Checked)
{
OpenDirectory(CustomDirFolder2);
}
if (CustomPathCheckBox3.Checked)
{
OpenDirectory(CustomDirFolder3);
}
if (DownloadsCheckBox.Checked)
{
OpenDirectory(downloadsPath);
}
}
Console.WriteLine("30s till console Disable");
Thread.Sleep(30000);
FreeConsole();
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred during cleanup: {ex.Message}");
}
}
// Helper method to clean the contents of a folder
private static void CleanFolder(string folderPath)
{
try
{
if (Directory.Exists(folderPath))
{
string[] files = Directory.GetFiles(folderPath);
foreach (string file in files)
{
try
{
File.Delete(file);
Console.WriteLine($"Deleted: {file}");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to delete {file}: {ex.Message}");
}
}
// Optionally delete subdirectories
string[] directories = Directory.GetDirectories(folderPath);
foreach (string dir in directories)
{
try
{
Directory.Delete(dir, true); // Delete directory and its contents
Console.WriteLine($"Deleted directory: {dir}");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to delete directory {dir}: {ex.Message}");
}
}
}
else
{
Console.WriteLine($"The folder {folderPath} does not exist.");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error cleaning folder {folderPath}: {ex.Message}");
}
}
public static void OpenDirectory(string directoryPath)
{
try
{
// Check if the directory exists
if (Directory.Exists(directoryPath))
{
// Open the directory using the default file explorer
Process.Start("explorer.exe", directoryPath);
Console.WriteLine($"Opened directory: {directoryPath}");
}
else
{
Console.WriteLine($"The directory {directoryPath} does not exist.");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred while opening the directory: {ex.Message}");
}
}
public static void DeleteVsFiles()
{
try
{
// Get all drives (e.g., C:, D:, E:)
string[] drives = Environment.GetLogicalDrives();
foreach (string drive in drives)
{
Console.WriteLine($"Searching drive: {drive}");
// Start the recursive search in each drive
SearchAndDeleteVsFiles(drive);
}
Console.WriteLine("Search and cleanup completed.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
// Recursively search for .vs files and delete them
private static void SearchAndDeleteVsFiles(string directory)
{
try
{
// Skip system directories and files that can't be accessed
if (directory.Contains("$RECYCLE.BIN") || directory.Contains("System Volume Information"))
return;
// Search for .vs files in the current directory
string[] vsFiles = Directory.GetFiles(directory, "*.vs", SearchOption.TopDirectoryOnly);
foreach (string file in vsFiles)
{
try
{
Console.WriteLine($"Deleting file: {file}");
File.Delete(file); // Delete the .vs file
}
catch (Exception ex)
{
Console.WriteLine($"Failed to delete {file}: {ex.Message}");
}
}
// Recursively search in subdirectories
string[] directories = Directory.GetDirectories(directory);
foreach (string dir in directories)
{
SearchAndDeleteVsFiles(dir); // Recurse into subdirectories
}
}
catch (UnauthorizedAccessException)
{
// Handle permissions issues (e.g., access denied on some folders)
Console.WriteLine($"Access denied to: {directory}");
}
catch (Exception ex)
{
Console.WriteLine($"Error processing {directory}: {ex.Message}");
}
}
public static void DeleteLogAndTraceFilesFromAllDrives()
{
try
{
// Get all drives on the system
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
// Skip the drive if it's not ready (e.g., CD/DVD drives or network drives)
if (!drive.IsReady) continue;
Console.WriteLine($"Searching drive: {drive.Name}");
// Delete .log files and .trc files from the root of the drive and its subdirectories
DeleteFilesByExtension(drive.Name, "*.log");
DeleteFilesByExtension(drive.Name, "*.trc");
}
Console.WriteLine("Log and trace files deletion complete.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
// Helper method to delete files by extension in all directories under a given directory
private static void DeleteFilesByExtension(string directoryPath, string filePattern)
{
try
{
// Get all files matching the pattern (recursively)
string[] files = Directory.GetFiles(directoryPath, filePattern, SearchOption.AllDirectories);
foreach (var file in files)
{
try
{
Console.WriteLine($"Deleting file: {file}");
File.Delete(file); // Delete the file
}
catch (Exception ex)
{
Console.WriteLine($"Could not delete {file}: {ex.Message}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error accessing directory {directoryPath}: {ex.Message}");
}
}
private void guna2CustomCheckBox1_Click(object sender, EventArgs e)
{
}
}
}