1.11M Members

FileSystemWatcher.Filter (only two files)

 
0
 

How do you set the FileSystemWatcher.Filter to check only two files.

Here's my code that doesn't even work.

watcher.Filter = "*.txt|*.ini"

Thanks Alot

 
1
 

Multiple filters are not supported. Syntax watcher.Filter = "*.txt" is correct but watcher.Filter = "*.txt|*.ini" is not.

A workaround could be to use two FileSystemWatcher objects, one for *.txt-files and one for *.ini-files, and use the same event handler(s) to handle both objects' events.

HTH

Unhnd_Exception
Deleted Member
 
0
 

You could set the Filter to everything *.* and check the extension in the
Event.

Private Sub FileSystemWatcher1_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Deleted

        If e.Name.EndsWith(".txt") Then
            MsgBox("Text File")
        ElseIf e.Name.EndsWith(".bmp") Then
            MsgBox("Bitmap File")
        End If

    End Sub
 
0
 

Multiple filters are not supported. Syntax watcher.Filter = "*.txt" is correct but watcher.Filter = "*.txt|*.ini" is not.

A workaround could be to use two FileSystemWatcher objects, one for *.txt-files and one for *.ini-files, and use the same event handler(s) to handle both objects' events.

HTH

It seems that your solution is the most elegant at the moment. Using two objects but the same handler.

You could set the Filter to everything *.* and check the extension in the
Event.

Private Sub FileSystemWatcher1_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Deleted

        If e.Name.EndsWith(".txt") Then
            MsgBox("Text File")
        ElseIf e.Name.EndsWith(".bmp") Then
            MsgBox("Bitmap File")
        End If

    End Sub

But that would check all files but only filter results.

Question Answered as of 3 Years Ago by Teme64 and Unhnd_Exception
Unhnd_Exception
Deleted Member
 
0
 

Its not magic. When you set an extension, the file watcher filters the extension for you and only sends a message if the extension is what you specified. When you don't specify it sends them all. Filtering it yourself does the same thing but eliminates a resource of having 2.

 
0
 

Its not magic. When you set an extension, the file watcher filters the extension for you and only sends a message if the extension is what you specified. When you don't specify it sends them all. Filtering it yourself does the same thing but eliminates a resource of having 2.

Actually I am trying to check only two files and not two file types. errors.txt and config.ini.

If it does the same thing, I might look into that. But what I want is more efficient, since the target folder contains lot of files for IO operations.

Thanks anyway.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article