Jedi
Jedi

Reputation: 153

Using FileSystemWatcher with specified file names

Is there a way to do action if file with specified name is created in for example, "C:\" using FileSystemWatcher?

Private Sub FileSystemWatcher1_changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
    If 
         'really don't know what to put here
    End If
End Sub

I will explain if you did not understand.

Upvotes: 1

Views: 322

Answers (1)

Steve
Steve

Reputation: 216253

Let's assume you have prepared your FileSystemWatcher1 with these properties

Dim FileSystemWatcher1 = New FileSystemWatcher()
FileSystemWatcher1.Path = "C:\"
FileSystemWatcher1.Filter = "*.*"
AddHandler FileSystemWatcher1.Created, AddressOf OnCreated
FileSystemWatcher1.EnableRaisingEvents = True
.....

Then you could write your event handler as you have already done above and look at the property of the FileSystemEventArgs argument passed to the event handler to know the exact name of the file created.

Private Shared Sub OnCreated(source As Object, e As FileSystemEventArgs)
    If e.Name.ToUpper() == "MYTEXTFILE.TXT" then
        ' do you code here '
    End If
End Sub

Upvotes: 3

Related Questions