Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top