Share via


How do I setup FileWatcher filter for multiple document types?

Question

Friday, January 28, 2011 10:27 PM

In using the FileWatcher class, is there a way that I can filter for several types of files? Or am I restricted to just one filter at a time? So i guess my question is how do I set multiple filters for various file extensions in one service? For instance, this is how am currently setting the filter for all document types.

this.fileSystemWatcher.Filter = "*";

How can I set this for specific document types? So if i want to monitor all txt, xls, and pdf files using one filter, how do I set this filter?

Thanks

All replies (4)

Saturday, January 29, 2011 7:25 AM âś…Answered | 1 vote

From the Remarks section here, http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.filter.aspx

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

 

You could set the filter to "" and then If (filename.endswith("*.txt") || filename.endswith("*.csv")), etc.

 

Here's an example of using a string array of desired file types, http://www.dreamincode.net/code/snippet2823.htm.

 

RTA.


Saturday, January 29, 2011 9:03 PM

On 2011-01-28 23:27, Spawn10 wrote:

> In using the FileWatcher class, is there a way that I can filter for

> several types of files? Or am I restricted to just one filter at a time?

> So i guess my question is how do I set multiple filters for various file

> extensions in one service? For instance, this is how am currently

> setting the filter for all document types.

>

> this.fileSystemWatcher.Filter ="*";

>

> How can I set this for specific document types? So if i want to monitor

> all txt, xls, and pdf files using one filter, how do I set this filter?

>

> Thanks

>

According to the doc

 

"To watch changes in all files, set the Filter property to an empty

string (""). To watch a specific file, set the Filter property to the

file name. For example, to watch for changes in the file MyDoc.txt, set

the Filter property to "MyDoc.txt". You can also watch for changes in a

certain type of file. For example, to watch for changes in any text

files, set the Filter property to "*.txt". Use of multiple filters such

as "*.txt|*.doc" is not supported."

 

So you need to setup one FileWatcher for each documenttype...

 

// Anders

 


Thursday, February 3, 2011 3:38 PM

Thanks for your help RogerTheAlien...the second link was quite helpful.


Friday, January 27, 2017 1:54 PM

You could create multiple FileSystemWatcher instances, one per file type you want to respond to, but that all point to the same Action.