Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileSystemWatcher: how to make it accept multiple file extensions?

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
566
US
Colleagues,
I need to monitor files of more than one type (e.g. XML, TXT, etc.)
I looked up FileSystemWatcher.Filters property - it's read only.
This is what I have:

2024-07-25FileSystemWatcher_Filter_Errs_ax2an8.jpg


What am I doing wrong?


Regards,

Ilya
 
As stated in the documentation: [highlight #FCE94F]Use of multiple filters such as "*.txt|*.doc" is not supported.[/highlight]
 
Right, not supported.
Any tip on how to get around this restriction?

Regards,

Ilya
 
But .. it seems to work - because besides the Filter Property it also has [highlight #FCE94F]Filters[/highlight] property, which is collection of filters:

To try it out, i took this example from documetation:

It has the setting to watch only TXT-files
Code:
...
watcher.Filter = "*.txt"
...
so i tried it how it works, and really - it watched in my directory only TXT files

Then i changed the code commenting out the Filter property and setting the [highlight #FCE94F]Filters[/highlight] property, i.e. adding 2 filters into the collection
Code:
...
'watcher.Filter = "*.txt"
watcher.Filters.Add("*.txt")
watcher.Filters.Add("*.log")

And now it works - it watches in my directory TXT and LOG files.
 
Ah, I was just about to say that some of the documentation is ... misleading, and mention that as of .Net Core 3.x and .Net 5 you can use Filters.Add method - but mikrom got in there first :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top