-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileWatcherService.cs
33 lines (31 loc) · 975 Bytes
/
FileWatcherService.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
using System;
using System.Configuration;
using System.IO;
using System.Threading;
namespace WindowsService.MultiThread
{
class FileWatcherService:IServiceThread
{
private void ProcessFile(object sender, FileSystemEventArgs e)
{
Console.WriteLine($"{e.ChangeType}, {e.Name}");
}
public bool ServiceStarted { get; set; }
public void DoWork()
{
if (ServiceStarted)
{
var watchPath = ConfigurationManager.AppSettings["WatchPath"];
var watcher = new FileSystemWatcher(watchPath) { EnableRaisingEvents = true };
watcher.Filter = "*.*";
watcher.Created += ProcessFile;
watcher.Deleted += ProcessFile;
watcher.Changed += ProcessFile;
}
else
{
Thread.CurrentThread.Abort();
}
}
}
}