I am trying to use the FileSystemWatcher class to move files around. I only want to move files if the LastWrite time (Date Modified) of a file changes in a particular directory. In other words; move the file if it is a new one. The problem is that when I add a new file to this directory, it moves the file multiple times instead of firing just once.
MoveFile is hit 4 times if I add a new file to the watched directory. Any help would be much appreciated.
Here is my code:
MoveFile is hit 4 times if I add a new file to the watched directory. Any help would be much appreciated.
Here is my code:
Code:
Public Sub StartWatcher()
Dim fwService As FileSystemWatcher
Dim gblpvFilters As String() = {"*.zip", "*.txt", "*.dat", "*.csv"}
For Each filter As String In gblpvFilters
fwService = New FileSystemWatcher()
fwService.Path = "C:\myFileWatcherTest\"
fwService.IncludeSubdirectories = True
fwService.NotifyFilter = (NotifyFilters.LastWrite)
fwService.Filter = filter
AddHandler fwService.Changed, AddressOf MoveFile
AddHandler fwService.Created, AddressOf MoveFile
fwService.EnableRaisingEvents = True
Next
End Sub
Private Sub MoveFile(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
File.Copy(e.FullPath, "C:\move\" & e.Name, True)
End Sub