How to set filter for FileSystemWatcher for multiple file types?

后端 未结 6 1131
醉酒成梦
醉酒成梦 2020-12-08 13:16

Everywhere I find these two lines of code used to set filter for file system watcher in samples provided..

FileSystemWatcher watcher = new FileSystemWatcher(         


        
6条回答
  •  没有蜡笔的小新
    2020-12-08 13:37

    You can't do that. The Filter property only supports one filter at a time. From the documentation:

    Use of multiple filters such as *.txt|*.doc is not supported.

    You need to create a FileSystemWatcher for each file type. You can then bind them all to the same set of FileSystemEventHandler:

    string[] filters = { "*.txt", "*.doc", "*.docx", "*.xls", "*.xlsx" };
    List watchers = new List();
    
    foreach(string f in filters)
    {
        FileSystemWatcher w = new FileSystemWatcher();
        w.Filter = f;
        w.Changed += MyChangedHandler;
        watchers.Add(w);
    }
    

提交回复
热议问题