FileWatcher

最近玩Wallpaper Engine停不下来,同事用Unity撸壁纸玩,需要监听配置文件修改,于是顺带学习了一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void StartWatch(string path, string filter, FileSystemEventHandler onChange)
{
if (watcher == null)
{
watcher = new FileSystemWatcher();
}

watcher.Path = path;
watcher.Filter = filter;

watcher.Changed += onChange;
watcher.Deleted += onChange;
watcher.Created += onChange;

watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite;

watcher.EnableRaisingEvents = true;
}