首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

FileSystemWatcher如何根据需求设置

2013-11-19 
FileSystemWatcher怎么根据需求设置this.fileSystemWatcher new FileSystemWatcher()this.fileSystemWa

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/

热点排行