日期:2014-05-17  浏览次数:20808 次

FileSystemWatcher怎么根据需求设置
this.fileSystemWatcher = new FileSystemWatcher();
this.fileSystemWatcher.Path = path; 设置路径,这个基本没有问题
this.fileSystemWatcher.Filter 这个问题比较大
如果同时监控不同文件类型比如:pdf,xls,xlsx,doc,txt
xls与xlsx可以设置为Filter="*.xls",但是同时要对pdf,xls就不会了,有人说过在事件里再加判断,
另外如果在path路径下文件夹(创建、删除、重命名)怎么避免呢?,因为我只想知道文件变化,比如这个文件夹里面的文件算是路径变化了吧,应该要发生事件,但是这个文件夹我不希望要触发事件
fileSystemWatcher

------解决方案--------------------
用多个FileSystemWatcher 
string[] filters = { "*.txt", "*.doc", "*.pdf", "*.xls", "*.xlsx" };
List<FileSystemWatcher> watchers = new List<FileSystemWatcher>;

foreach(string f in filters)
{
    FileSystemWatcher w = new FileSystemWatcher();
    w.Filter = f;
    w.Path = path;
    w.Changed = MyChangedHandler;
    watchers.Add(w);
}
  
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/