design to overcome filesystemwatcher filter limitation

E

emailtonitin

I am facing a design issue in my code related to the
filesystemwatcher's filter property.
I figured out that the filter won't accept multiple patterns for eg.
"*.txt, *.csv" don't work.

To workaround this issue, I have 2 options:
1. To create multiple instances of filesystemwatcher class and hook all
of them to the same event handlers.
2. To use 1 instance of filesystemwatcher to listen to all files using
blank filter. And in the event handler sort out the files that I need.
for eg. If (filename.endswith("*.txt") || filename.endswith("*.csv"))
//do stuff

My question is which approach is going to be more efficient, provided:
The list of file extensions that I need to watch are configurable (are
subject to change/number of extensions to be watched may go up)

Want to know your thoughts before deciding eitherway.
Thanks,
Nitin
 
G

Greg Young

Personally I would use one and a regex to do the filterring but perhaps
someone knows a better way.

Cheers,

Greg
 
J

Jon Shemitz

Greg said:
Personally I would use one and a regex to do the filterring but perhaps
someone knows a better way.

That's probably the best approach, imho. Multiple watchers means
multiple OS waits: it's definitely going to be more efficient to watch
a whole directory than to watch multiple patterns.

Regex isn't going to be much (if any) slower than looping through an
array of masks and comparing each mask to
Path.GetExtension().ToLower() ... and it will be much smaller code.
 
E

emailtonitin

Thanks guys.
Yes, using multiple watchers didn't seem right to me too.
Ok, I'll go the single watcher + regex way.

As a sidenote, the lack of support for multiple patterns in the filter
property of the filesystemwatcher seems more like an incomplete
functionality of the class, prevelant since framework version 1.1.
Lets hope that some future release takes care of it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top