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

Recommended Answers

All 5 Replies

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

Member Avatar for Unhnd_Exception

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

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.

Member Avatar for Unhnd_Exception

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.