-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-monitor.cs
80 lines (65 loc) · 2.45 KB
/
file-monitor.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
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading;
namespace FileMonitor
{
internal class Program
{
private static void OnCreated(object sender, FileSystemEventArgs e) =>
Console.WriteLine($"Created: {e.FullPath}");
private static void OnDeleted(object sender, FileSystemEventArgs e) =>
Console.WriteLine($"Deleted: {e.FullPath}");
private static void OnRenamed(object sender, RenamedEventArgs e) =>
Console.WriteLine($"Renamed:\n Old: {e.OldFullPath}\n New: {e.FullPath}");
static void MonitorPath(String path)
{
var watcher = new FileSystemWatcher(@path);
watcher.NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.Security
| NotifyFilters.Size;
watcher.Created += OnCreated;
watcher.Deleted += OnDeleted;
watcher.Renamed += OnRenamed;
watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;
while (true) ;
}
private static List<Thread> threads = new List<Thread>();
static int Main()
{
Console.WriteLine(">>> Starting File Monitor");
Console.CancelKeyPress += delegate
{
foreach (Thread thread in threads)
{
Console.WriteLine("[+] Stopping monitor for " + thread.Name);
thread.Abort();
}
};
List<string> paths = new List<string>
{
Path.GetTempPath(),
// add other paths you wish to monitor here
};
foreach (string path in paths)
{
Thread t = new Thread(() => MonitorPath(path));
t.Name = "Monitor: " + path;
threads.Add(t);
t.Start();
}
foreach (Thread thread in threads)
{
thread.Join();
}
Console.WriteLine(">>> File Monitor Stopped");
return 0;
}
}
}