/
frmMain.cs
265 lines (232 loc) · 9.64 KB
/
frmMain.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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.IO;
using CustomUIControls;
using System.Diagnostics;
using System.Threading;
using System.Net.Mail;
using System.Net;
namespace FolderWatch
{
public partial class frmMain : Form
{
private Dictionary<string, FileSystemWatcher> watchers;
private int position = 0;
public frmMain()
{
InitializeComponent();
watchers = new Dictionary<string, FileSystemWatcher>();
//Cargar Configuracion
CargarConfig();
}
protected override void OnClosing(CancelEventArgs e)
{
//Guardar Configuracion
GuardarConfig();
base.OnClosing(e);
}
private void CargarConfig()
{
if (!File.Exists("config.cfg"))
return;
StreamReader str = new StreamReader("config.cfg");
string linea = str.ReadLine();
Regex config=new Regex("\\<(?<nombre>.+?)\\>\\((?<valor>.+?)\\)");
Match m;
while (linea != "" && linea!=null)
{
if (config.IsMatch(linea))
{
m = config.Match(linea);
if (m.Groups["nombre"].Value.ToLower() == "ee")
chkEmail.Checked = Boolean.Parse(m.Groups["valor"].Value);
if (m.Groups["nombre"].Value.ToLower() == "ma")
chkAlerta.Checked = Boolean.Parse(m.Groups["valor"].Value);
if (m.Groups["nombre"].Value.ToLower() == "rs")
chkSonido.Checked = Boolean.Parse(m.Groups["valor"].Value);
if (m.Groups["nombre"].Value.ToLower() == "cr")
chkCrear.Checked = Boolean.Parse(m.Groups["valor"].Value);
if (m.Groups["nombre"].Value.ToLower() == "el")
chkEliminar.Checked = Boolean.Parse(m.Groups["valor"].Value);
if (m.Groups["nombre"].Value.ToLower() == "mo")
chkModificar.Checked = Boolean.Parse(m.Groups["valor"].Value);
if (m.Groups["nombre"].Value.ToLower() == "emails")
txtEmail.Text = m.Groups["valor"].Value;
if (m.Groups["nombre"].Value.ToLower() == "servidor")
txtServidor.Text = m.Groups["valor"].Value;
if (m.Groups["nombre"].Value.ToLower() == "ext")
txtExtension.Text = m.Groups["valor"].Value;
if (m.Groups["nombre"].Value.ToLower() == "folder")
AgregaWatcher(m.Groups["valor"].Value);
}
linea = str.ReadLine();
}
str.Close();
}
private void GuardarConfig()
{
StreamWriter str = new StreamWriter("config.cfg",false);
string formato="<{0}>({1})";
str.WriteLine(String.Format(formato, "ee", chkEmail.Checked.ToString()));
str.WriteLine(String.Format(formato, "ma", chkAlerta.Checked.ToString()));
str.WriteLine(String.Format(formato, "rs", chkSonido.Checked.ToString()));
str.WriteLine(String.Format(formato, "cr", chkCrear.Checked.ToString()));
str.WriteLine(String.Format(formato, "el", chkEliminar.Checked.ToString()));
str.WriteLine(String.Format(formato, "mo", chkModificar.Checked.ToString()));
str.WriteLine(String.Format(formato, "emails", txtEmail.Text));
str.WriteLine(String.Format(formato, "servidor", txtServidor.Text));
str.WriteLine(String.Format(formato, "ext", txtExtension.Text));
foreach (Object obj in lstDirectorios.Items)
{
str.WriteLine(String.Format(formato, "folder",obj.ToString()));
}
str.Close();
}
private void btnAgregar_Click(object sender, EventArgs e)
{
if (dlgFolder.ShowDialog() == DialogResult.OK)
{
AgregaWatcher(dlgFolder.SelectedPath);
}
}
private void AgregaWatcher(string folder)
{
if (!watchers.ContainsKey(folder))
{
lstDirectorios.Items.Add(folder);
FileSystemWatcher fsw = new FileSystemWatcher(folder);
fsw.Created += new FileSystemEventHandler(FileSystemWatcher_Event);
fsw.Changed += new FileSystemEventHandler(FileSystemWatcher_Event);
fsw.Renamed += new RenamedEventHandler(FileSystemWatcher_Event);
fsw.Deleted += new FileSystemEventHandler(FileSystemWatcher_Event);
fsw.EnableRaisingEvents = true;
fsw.SynchronizingObject = this;
}
}
private void btnEliminar_Click(object sender, EventArgs e)
{
if (lstDirectorios.SelectedItem != null)
{
string file=lstDirectorios.SelectedItem.ToString();
FileSystemWatcher fsw = watchers[file];
fsw.Dispose();
watchers.Remove(file);
lstDirectorios.Items.Remove(lstDirectorios.SelectedItem);
}
}
private void FileSystemWatcher_Event(object sender, FileSystemEventArgs e)
{
string[] exts = txtExtension.Text.Replace("*","").Split(';');
bool alert = false;
foreach (string s in exts)
{
if (e.Name.Contains(s))
{
alert = true;
break;
}
}
if (!alert)
return;
if (chkCrear.Checked && e.ChangeType == WatcherChangeTypes.Created)
{
Aviso("Creado", e.Name, e.FullPath);
}
if (chkEliminar.Checked && e.ChangeType == WatcherChangeTypes.Deleted)
{
Aviso("Eliminado", e.Name, e.FullPath);
}
if (chkModificar.Checked && (e.ChangeType == WatcherChangeTypes.Changed || e.ChangeType == WatcherChangeTypes.Renamed))
{
Aviso("Modificado", e.Name, e.FullPath);
}
}
private void Aviso(string descripcion, string archivo, string direccion)
{
if (chkEmail.Checked)
{
if (txtServidor.Text != "" && txtEmail.Text!="")
{
SmtpClient client = new SmtpClient(txtServidor.Text);
client.Credentials = new NetworkCredential("HMOMARTINREA\\ecarrillo", "Troya2016");
MailMessage mail = new MailMessage("FolderWatchAlert@FWA.x", txtEmail.Text);
mail.Subject = "Archivo " + descripcion + ": " + archivo ;
mail.Body = direccion;
mail.BodyEncoding = Encoding.UTF8;
mail.IsBodyHtml = false;
try
{
client.Send(mail);
}
catch (Exception ex)
{
string es = ex.ToString();
}
}
}
if (chkAlerta.Checked)
{
TaskbarNotifier tn = new TaskbarNotifier();
tn.SetBackgroundBitmap("skin.bmp",
Color.FromArgb(255, 0, 255));
tn.SetCloseBitmap("close.bmp",
Color.FromArgb(255, 0, 255), new Point(127, 8));
tn.TitleRectangle = new Rectangle(40, 9, 70, 25);
tn.ContentRectangle = new Rectangle(8, 20, 133, 88);
tn.ReShowOnMouseOver = true;
tn.StackPosition = position++;
tn.ContentClick += new EventHandler(Notifier_ContentClick);
tn.VisibleChanged += new EventHandler(Notifier_VisibleChanged);
tn.Tag = direccion;
tn.Visible = true;
tn.Show(descripcion , archivo
, 250, 1500000000, 250);
Thread.Sleep(1000);
}
if (chkSonido.Checked)
{
WSounds ws = new WSounds();
ws.Play("beep.wav", ws.SND_FILENAME | ws.SND_ASYNC);
}
}
void Notifier_VisibleChanged(object sender, EventArgs e)
{
TaskbarNotifier tn = sender as TaskbarNotifier;
if (tn.Visible == false)
{
if (position == tn.StackPosition + 1)
position = 0;
}
}
void Notifier_ContentClick(object sender, EventArgs e)
{
TaskbarNotifier tn = sender as TaskbarNotifier;
string archivo = (string)tn.Tag;
ProcessStartInfo process = new ProcessStartInfo();
process.FileName = "explorer.exe";
process.Arguments = "/select," + archivo;
process.RedirectStandardInput = false;
process.RedirectStandardOutput = false;
process.RedirectStandardError = false;
process.CreateNoWindow = false;
process.UseShellExecute = false;
Process ex = Process.Start(process);
tn.Close();
}
private void txtExtension_TextChanged(object sender, EventArgs e)
{
}
private void txtExtension_KeyUp(object sender, KeyEventArgs e)
{
}
private void btnAplicar_Click(object sender, EventArgs e)
{
}
}
}