How to set filter for FileSystemWatcher for multiple file types?

后端 未结 6 1121
醉酒成梦
醉酒成梦 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:44

    You could also filter by using FileInfo by comparing to the string of the extension you're looking for.

    For example the handler for a file changed event could look like:

    void File_Changed(object sender, FileSystemEventArgs e)
    {
        FileInfo f = new FileInfo(e.FullPath);
    
        if (f.Extension.Equals(".jpg") || f.Extension.Equals(".png"))
        {
           //Logic to do whatever it is you're trying to do goes here               
        }
    }
    

提交回复
热议问题