Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set filter for FileSystemWatcher for multiple file types?

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

FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Filter = "*.txt"; //or watcher.Filter = "*.*"; 

But I want my watcher to monitor more file types, but not all. How can I achieve this:

//watcher.Filter = "*.txt" | "*.doc" | "*.docx" | "*.xls" | "*.xlsx"; 

I tried these:

 watcher.Filter = "*.txt|*.doc|*.docx|*.xls|*.xlsx";   // and  watcher.Filter = "*.txt;*.doc;*.docx;*.xls;*.xlsx*"; 

Both did not work. This is just basics but I miss it. Thanks..

like image 893
nawfal Avatar asked Aug 06 '11 06:08

nawfal


People also ask

Is FileSystemWatcher multithreaded?

Nope, filesystemwatchers run on their own thread.

Which of the following are types of changes that can be detected by the FileSystemWatcher?

The FileSystemWatcher lets you detect several types of changes in a directory or file, like the 'LastWrite' date and time, changes in the size of files or directories etc. It also helps you detect if a file or directory is deleted, renamed or created.

What are the guidelines to be followed to avoid missing events while using FileSystemWatcher?

Note that a FileSystemWatcher may miss an event when the buffer size is exceeded. To avoid missing events, follow these guidelines: Increase the buffer size by setting the InternalBufferSize property. Avoid watching files with long file names, because a long file name contributes to filling up the buffer.

How do I change the filter property in FileSystemWatcher?

The Filter property can be changed after the FileSystemWatcher object has started receiving events. For more information about filtering out unwanted notifications, see the NotifyFilter, IncludeSubdirectories, and InternalBufferSize properties. Filter accepts wildcards for matching files, as shown in the following examples.

How many events does FileSystemWatcher raise per copy of a file?

It is normal for FileSystemWatcher to raise multiple events for file copies. Generally at least one create, and one change event is raised, plus there might be more. If an antivirus program is running there can be even more. 10 is unusual, but I don't see any issue with your code which only raises three events per file on my system.

Is it possible to use multiple file types in a filter?

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:

How do I watch all files in a directory?

File System Watcher. Filter Property System. IO Gets or sets the filter string used to determine what files are monitored in a directory. The filter string. The default is "*.*" (Watches all files.) The following example creates a FileSystemWatcher to watch the directory specified at run time.


2 Answers

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<FileSystemWatcher> watchers = new List<FileSystemWatcher>();  foreach(string f in filters) {     FileSystemWatcher w = new FileSystemWatcher();     w.Filter = f;     w.Changed += MyChangedHandler;     watchers.Add(w); } 
like image 58
Anders Abel Avatar answered Oct 11 '22 14:10

Anders Abel


There is a workaround.

The idea is to watch for all extensions and then in the OnChange event, filter out to desired extensions:

FileSystemWatcher objWatcher = new FileSystemWatcher();  objWatcher.Filter = "*.*";  objWatcher.Changed += new FileSystemEventHandler(OnChanged);   private static void OnChanged(object source, FileSystemEventArgs e)  {      // get the file's extension      string strFileExt = getFileExt(e.FullPath);       // filter file types      if (Regex.IsMatch(strFileExt, @"\.txt)|\.doc", RegexOptions.IgnoreCase))      {          Console.WriteLine("watched file type changed.");      }  }  
like image 20
Mrchief Avatar answered Oct 11 '22 14:10

Mrchief