-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
205 lines (176 loc) · 7.65 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
namespace cl.trends.pci.WEBPCI.BorradoSeguroWebPCI
{
class Program
{
static readonly String ERASER_ARGUMENTS = "erase /method=b1bfab4a-31d3-43a5-914c-e9892c78afd8 /target file=";
static readonly DateTime TODAY = DateTime.Now;
static String WORKSPACE = @"C:\TEST 3\";
//static readonly String LOG_FILE = @"log.txt";
static String ERASER_PATH = @"C:\Program Files\Eraser\Eraser.exe";
static int RETENTION = 15;
static string PARAMETERS_FILE;
static void Main(string[] args)
{
try
{
if (args.Length == 0)
{
System.Console.WriteLine("Ingrese la ruta del archivo de parámetros");
}
PARAMETERS_FILE = args[0];
Log("Leyendo parámetros", EventLogEntryType.Information);
readParameters();
Log("Parámetros leídos", EventLogEntryType.Information);
if (!EraserExists())
{
String msg = "No se ha encontrado una instalación de Eraser con la ruta configurada " + ERASER_PATH + ". contacte al Administrador.";
Log(msg, EventLogEntryType.Error);
throw new System.ApplicationException(msg);
}
int cont = 0;
foreach (String path in GetPathsInWorkSpace())
{
if (IsEligibleForErasure(path))
{
CallEraserOnFile(path);
cont++;
}
}
Log("Archivos eliminados: " + cont + ". Programa terminado", EventLogEntryType.Information);
}
catch (Exception e)
{
Log(e.Message, EventLogEntryType.Error);
throw e;
}
System.Environment.Exit(1);
}
static void readParameters()
{
try
{
FileStream fileStream = new FileStream(PARAMETERS_FILE, FileMode.Open);
using (StreamReader reader = new StreamReader(fileStream))
{
while (reader.Peek() >= 0)
{
string line = reader.ReadLine();
string[] tokens = line.Split('=');
if (tokens.Length != 2)
{
String msg = "Formato no válido. Los parámetros deben ser especificados en la forma: [NOMBRE] = [VALOR]";
Log(msg, EventLogEntryType.Error);
throw new System.ApplicationException(msg);
}
switch (tokens[0])
{
case "ERASER_HOME":
ERASER_PATH = tokens[1] + @"Eraser.exe";
break;
case "PATHS":
WORKSPACE = tokens[1];
break;
case "RETENTION":
RETENTION = Int32.Parse(tokens[1]);
break;
default:
String msg = "Parámetro no válido. Valores aceptados: ERASER_HOME, PATHS, RETENTION";
Log(msg, EventLogEntryType.Error);
throw new System.ApplicationException(msg);
}
}
}
}
catch(FileNotFoundException e)
{
String msg = "El archivo de parámetros 'parameters.txt' no existe. Debe crear este archivo en la ruta donde se encuentra el ejecutable del aplicativo.";
Log(msg, EventLogEntryType.Error);
throw new System.ApplicationException(msg);
}
catch(FormatException e2)
{
String msg = "Formato no válido. Los parámetros deben ser especificados en la forma: [NOMBRE] = [VALOR]";
Log(msg, EventLogEntryType.Error);
throw new System.ApplicationException(msg);
}
}
static List<String> GetPathsInWorkSpace()
{
String drive = Path.GetPathRoot(WORKSPACE);
if (!Directory.Exists(drive))
{
String msg = "Se esperaba disco de red montado en unidad F:\\ Por favor revise que el disco esté " +
"conectado en la unidad indicada. Si el problema persiste contacte al Administrador.";
Log(msg, EventLogEntryType.Error);
throw new System.ApplicationException(msg);
}
if (!Directory.Exists(WORKSPACE))
{
String msg = "Se esperaba directorio de descargas. Cree un directorio de descargas correspondiente a " + WORKSPACE +
". Si el problema persiste contacte al Administrador.";
Log(msg, EventLogEntryType.Error);
throw new System.ApplicationException(msg);
}
return Directory.GetFiles(WORKSPACE).ToList();
}
static Boolean IsEligibleForErasure(String path)
{
DateTime lastModified = File.GetLastAccessTime(path);
if ((TODAY - lastModified).TotalDays > RETENTION)
{
return true;
}
return false;
}
static Boolean EraserExists()
{
return File.Exists(ERASER_PATH);
}
static void CallEraserOnFile(String path)
{
try
{
Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = ERASER_PATH;
process.StartInfo.Arguments = ERASER_ARGUMENTS + '"' + path + '"'; //argument
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.CreateNoWindow = true; //not diplay a windows
process.StartInfo.Verb = "runas";
process.Start();
//process.StandardOutput.ReadToEnd();
//process.WaitForExit();
while (File.Exists(path))
{
Thread.Sleep(2000);
}
if (!process.HasExited)
{
process.Kill();
}
string output = "Archivo " + path + " borrado exitosamente."; //The output result
Log(output, EventLogEntryType.Information);
}
catch (Exception e)
{
throw new System.ApplicationException(e.Message);
}
}
static void Log(String message, EventLogEntryType level)
{
using (EventLog eventLog = new EventLog("Application"))
{
eventLog.Source = "Trends Tecnología";
eventLog.WriteEntry(message, level, 9999, 19 /*Archive Task*/);
}
}
}
}