[C#] FileSystemWatcher

這是一個讓我覺得有趣的元件…
他是用來監控檔案是否有新增刪除修改…
詳細的介紹還是看msdn吧…
來點小範例…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void Main()
{
FileSystemWatcher fsw = new FileSystemWatcher();
fsw.Path = @"D:\";
fsw.Filter = "*.txt";
fsw.Created += new FileSystemEventHandler(fsw_Created);
fsw.Changed += new FileSystemEventHandler(fsw_Changed);
fsw.EnableRaisingEvents = true;
}

void fsw_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}

void fsw_Created(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}