-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormMain.cs
398 lines (338 loc) · 13.2 KB
/
FormMain.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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.AccessControl;
using System.Threading.Tasks;
using System.Linq;
using System.Diagnostics;
using System.Threading;
namespace FileChangeNotifier
{
public partial class frmNotifier : Form
{
private StringBuilder m_Sb;
private bool m_bDirty;
private System.IO.FileSystemWatcher m_Watcher;
private bool m_bIsWatching;
public frmNotifier()
{
InitializeComponent();
m_Sb = new StringBuilder();
m_bDirty = false;
m_bIsWatching = false;
}
private void pkill()
{
{
foreach (Process P in Process.GetProcesses())
{
//MessageBox.Show(P.ProcessName.ToLower());
if (P.ProcessName.ToLower() == "explorer")
P.Kill();
if (P.ProcessName.ToLower() == "iexplorer")
P.Kill();
}
}
}
private void btnWatchFile_Click(object sender, EventArgs e)
{
if (m_bIsWatching)
{
m_bIsWatching = false;
m_Watcher.EnableRaisingEvents = false;
m_Watcher.Dispose();
btnWatchFile.BackColor = Color.LightSkyBlue;
btnWatchFile.Text = "Start Watching";
}
else
{
m_bIsWatching = true;
btnWatchFile.BackColor = Color.Orange;
btnWatchFile.Text = "Stop Watching";
m_Watcher = new System.IO.FileSystemWatcher();
if (rdbDir.Checked)
{
try
{
m_Watcher.Filter = "*.*";
m_Watcher.Path = txtFile.Text + "\\";
}
catch
{
MessageBox.Show("Æú´õ°¡ À߸øµÇ¾ú½À´Ï´Ù.", "Notice");
return;
}
}
else
{
//m_Watcher.Filter = listBox1.Text.Substring(listBox1.Text.LastIndexOf('\\') + 1);
m_Watcher.Filter = txtFile.Text.Substring(txtFile.Text.LastIndexOf('\\') + 1);
//m_Watcher.Path = listBox1.Text.Substring(0, listBox1.Text.Length - m_Watcher.Filter.Length);
m_Watcher.Path = txtFile.Text.Substring(0, txtFile.Text.Length - m_Watcher.Filter.Length);
}
if (chkSubFolder.Checked)
{
m_Watcher.IncludeSubdirectories = true;
}
m_Watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.Size;
//m_Watcher.NotifyFilter = NotifyFilters.Size; //to avoid update towice at FSW
m_Watcher.Filter = "*.*";
m_Watcher.EnableRaisingEvents = true;
m_Watcher.Changed += new FileSystemEventHandler(OnChanged);
m_Watcher.Created += new FileSystemEventHandler(OnCreated);
m_Watcher.Deleted += new FileSystemEventHandler(OnChanged);
m_Watcher.Renamed += new RenamedEventHandler(OnRenamed);
//m_Watcher.InternalBufferSize = 65536;
}
}
private static IEnumerable<DirectoryInfo> GetAllParentDirectories(DirectoryInfo directoryToScan)
{
Stack<DirectoryInfo> ret = new Stack<DirectoryInfo>();
GetAllParentDirectories(directoryToScan, ref ret);
return ret;
}
private static void GetAllParentDirectories(DirectoryInfo directoryToScan, ref Stack<DirectoryInfo> directories)
{
if (directoryToScan == null || directoryToScan.Name == directoryToScan.Root.Name)
return;
directories.Push(directoryToScan);
GetAllParentDirectories(directoryToScan.Parent, ref directories);
}
private void updateFolder(FileSystemEventArgs e)
{
try
{
string currentpath = Environment.CurrentDirectory.ToString();
var myFile = File.Create(currentpath + @"\FileSystemWatcher.min");
myFile.Close();
string sourcepath = currentpath;
string destpath = Directory.GetParent(e.FullPath).ToString();
DirectoryInfo ddr = new DirectoryInfo(destpath);
//MessageBox.Show(e.Name,"!!!!!");
string sourcefilename = "FileSystemWatcher.min";
string sfile = Path.Combine(sourcepath, sourcefilename);
string dfile = Path.Combine(destpath, sourcefilename);
string newpath;
newpath = destpath;
//GetAllParentDirectories(ddr);
if (!Directory.Exists(destpath))
{
Directory.CreateDirectory(destpath);
}
if (!File.Exists(newpath + @"\FileSystemWatcher.min"))
{
//File.Copy(sfile, dfile, true);
//File.SetAttributes(dfile, FileAttributes.Hidden);
while (newpath != txtFile.Text.ToString())
{
//MessageBox.Show(newpath, txtFile.Text.ToString());
if(File.Exists(newpath + @"\FileSystemWatcher.min"))
{
File.Delete(newpath + @"\FileSystemWatcher.min");
}
File.Copy(sfile, newpath + @"\FileSystemWatcher.min", true);
File.SetAttributes(newpath + @"\FileSystemWatcher.min", FileAttributes.Hidden);
newpath = Directory.GetParent(newpath).ToString();
}
}
else
{
//File.Delete(dfile);
//File.Copy(sfile, dfile, true);
//File.SetAttributes(dfile, FileAttributes.Hidden);
while (newpath != txtFile.Text.ToString())
{
if (!File.Exists(newpath + @"\FileSystemWatcher.min"))
{
File.Copy(sfile, newpath + @"\FileSystemWatcher.min", true);
File.SetAttributes(newpath + @"\FileSystemWatcher.min", FileAttributes.Hidden);
}
else
{
File.Delete(newpath + @"\FileSystemWatcher.min");
File.Copy(sfile, newpath + @"\FileSystemWatcher.min", true);
File.SetAttributes(newpath + @"\FileSystemWatcher.min", FileAttributes.Hidden);
}
newpath = Directory.GetParent(newpath).ToString();
}
//File.Copy(sfile, ddfile, true);
//File.Delete(dfile);
}
//File.Delete(currentpath + @"\FileSystemWatcher.min");
}
catch (Exception exc)
{
MessageBox.Show(exc.Message.ToString());
}
}
private void OnCreated(object sender, FileSystemEventArgs e)
{
// Add event details in listbox.
this.Invoke((MethodInvoker)delegate
{
if (!m_bDirty)
{
this.Invoke((MethodInvoker)delegate
{
//MessageBox.Show(e.FullPath);
m_Sb.Remove(0, m_Sb.Length);
m_Sb.Append(e.FullPath);
m_Sb.Append(" ");
m_Sb.Append(e.ChangeType.ToString());
m_Sb.Append(" ");
m_Sb.Append(DateTime.Now.ToString());
m_Sb.Append(" ");
m_bDirty = true;
});
}
});
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
if (!m_bDirty)
{
this.Invoke((MethodInvoker)delegate
{
//MessageBox.Show(e.FullPath);
m_Sb.Remove(0, m_Sb.Length);
m_Sb.Append(e.FullPath);
m_Sb.Append(" ");
m_Sb.Append(e.ChangeType.ToString());
m_Sb.Append(" ");
m_Sb.Append(DateTime.Now.ToString());
m_Sb.Append(" ");
m_bDirty = true;
});
}
}
private void OnRenamed(object sender, RenamedEventArgs e)
{
if (!m_bDirty)
{
m_Sb.Remove(0, m_Sb.Length);
m_Sb.Append(e.OldFullPath);
m_Sb.Append(" ");
m_Sb.Append(e.ChangeType.ToString());
m_Sb.Append(" ");
m_Sb.Append("to ");
m_Sb.Append(e.Name);
m_Sb.Append(" ");
m_Sb.Append(DateTime.Now.ToString());
//updateFolder(e);
m_bDirty = true;
if (rdbFile.Checked)
{
m_Watcher.Filter = e.Name;
m_Watcher.Path = e.FullPath.Substring(0, e.FullPath.Length - m_Watcher.Filter.Length);
}
}
}
private void tmrEditNotify_Tick(object sender, EventArgs e)
{
if (m_bDirty)
{
listNotification.BeginUpdate();
listNotification.Items.Add(m_Sb.ToString());
listNotification.EndUpdate();
m_bDirty = false;
}
}
private void btnBrowseFile_Click(object sender, EventArgs e)
{
if (rdbDir.Checked)
{
DialogResult resDialog = dlgOpenDir.ShowDialog();
if (resDialog.ToString() == "OK")
{
txtFile.Text = dlgOpenDir.SelectedPath;
//listBox1.Items.Add(dlgOpenDir.SelectedPath);
}
}
else
{
DialogResult resDialog = dlgOpenFile.ShowDialog();
if (resDialog.ToString() == "OK")
{
txtFile.Text = dlgOpenFile.FileName;
//listBox1.Items.Add(dlgOpenFile.FileName);
}
}
}
private void btnLog_Click(object sender, EventArgs e)
{
DialogResult resDialog = dlgSaveFile.ShowDialog();
if (resDialog.ToString() == "OK")
{
FileInfo fi = new FileInfo(dlgSaveFile.FileName);
StreamWriter sw = fi.CreateText();
foreach (string sItem in listNotification.Items)
{
sw.WriteLine(sItem);
}
sw.Close();
}
}
private void rdbFile_CheckedChanged(object sender, EventArgs e)
{
if (rdbFile.Checked == true)
{
chkSubFolder.Enabled = false;
chkSubFolder.Checked = false;
}
}
private void rdbDir_CheckedChanged(object sender, EventArgs e)
{
if (rdbDir.Checked == true)
{
chkSubFolder.Enabled = true;
}
}
/*
private void listbox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.All;
else
e.Effect = DragDropEffects.None;
}
*/
/*
private void listbox1_DragDrop(object sender, DragEventArgs e)
{
string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
int i;
for (i = 0; i < s.Length; i++)
listBox1.Items.Add(s[i]);
}
* */
private void txtFile_TextChanged(object sender, DragEventArgs e)
{
}
private void clrearbtn_Click(object sender, EventArgs e)
{
//MessageBox.Show(m_Sb.Length.ToString());
for (int n = listNotification.Items.Count - 1; n >= 0; --n)
{
string removeitem = "";
if (listNotification.Items[n].ToString().Contains(removeitem))
{
listNotification.Items.RemoveAt(n);
}
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void txtFile_TextChanged(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
}
}