-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClearTempFiles.cs
More file actions
147 lines (130 loc) · 5.26 KB
/
Copy pathClearTempFiles.cs
File metadata and controls
147 lines (130 loc) · 5.26 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
using System;
using System.IO;
namespace ClearFivem
{
internal class ClearTempFiles
{
static public void Clear()
{
string tempPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp");
if (Directory.Exists(tempPath))
{
Console.WriteLine("Cleaning temporary files...");
var directories = Directory.GetDirectories(tempPath);
var files = Directory.GetFiles(tempPath);
int totalItems = directories.Length + files.Length;
int cleanedItems = 0;
int failedItems = 0;
long totalSizeFreed = 0;
foreach (var dir in directories)
{
try
{
if (Directory.Exists(dir))
{
// Calcular el tamaño antes de eliminar
totalSizeFreed += GetDirectorySize(dir);
Directory.Delete(dir, true);
}
cleanedItems++;
}
catch (UnauthorizedAccessException)
{
failedItems++; // Contar los errores de acceso denegado
}
catch (IOException)
{
failedItems++; // Contar los errores si el archivo está en uso
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error deleting directory {dir}: {ex.Message}");
failedItems++;
}
ShowProgress(cleanedItems + failedItems, totalItems);
}
foreach (var file in files)
{
try
{
if (File.Exists(file))
{
// Acumular el tamaño del archivo antes de eliminarlo
FileInfo fileInfo = new FileInfo(file);
totalSizeFreed += fileInfo.Length;
File.Delete(file);
}
cleanedItems++;
}
catch (UnauthorizedAccessException)
{
failedItems++; // Contar los errores de acceso denegado
}
catch (IOException)
{
failedItems++; // Contar los errores si el archivo está en uso
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error deleting file {file}: {ex.Message}");
failedItems++;
}
ShowProgress(cleanedItems + failedItems, totalItems);
}
Console.WriteLine("\nTemporary files cleaned successfully.");
Console.WriteLine($"Total items cleaned: {cleanedItems}");
Console.WriteLine($"Total items failed to clean: {failedItems}");
Console.WriteLine($"Total space freed: {FormatSize(totalSizeFreed)}.");
}
else
{
Console.WriteLine("Temporary files directory does not exist.");
}
}
// Calcular el tamaño total de un directorio (incluidos subdirectorios)
private static long GetDirectorySize(string dirPath)
{
long size = 0;
try
{
// Sumar el tamaño de los archivos en el directorio actual
DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
foreach (var file in dirInfo.GetFiles())
{
size += file.Length;
}
// Sumar el tamaño de los archivos en subdirectorios
foreach (var dir in dirInfo.GetDirectories())
{
size += GetDirectorySize(dir.FullName);
}
}
catch
{
// Silenciar cualquier error al obtener el tamaño
}
return size;
}
// Método para mostrar el progreso en la consola
private static void ShowProgress(int processedItems, int totalItems)
{
Console.CursorLeft = 0;
int barWidth = 50;
int progressBars = processedItems * barWidth / (totalItems > 0 ? totalItems : 1);
Console.Write($"[{new string('=', progressBars)}{new string(' ', barWidth - progressBars)}] {processedItems}/{totalItems} Items processed");
}
// Método para formatear el tamaño en bytes a KB, MB o GB
static public string FormatSize(long sizeInBytes)
{
string[] sizeUnits = { "B", "KB", "MB", "GB", "TB" };
double size = sizeInBytes;
int unitIndex = 0;
while (size >= 1024 && unitIndex < sizeUnits.Length - 1)
{
size /= 1024;
unitIndex++;
}
return $"{size:F2} {sizeUnits[unitIndex]}";
}
}
}