Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileSystemWatcher - AddHandler

Status
Not open for further replies.

bmpsu

Programmer
Jan 13, 2005
128
US
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:
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
 
It's hard to help you when we don't have the file names etc you are using for testing. I suggest tracing through your code to see exactly what is happening.
 
When testing I am just using any file I have available with an extension in the defined String() above - gblpvFilters. [ex: myTest.txt].

I have been doing some reading. I find that using Notepad to test with may be my first problem. From my reading: Notepad performs multiple file system actions during the writing process. Notepad writes to the disk in batches that create the content of the file and then the file attributes.

So it seems by design that multiple events are in fact triggered when using this test scenario. This may be true in other scenarios not using Notepad. I may need to code for this and maybe do some type of check on the last time an event was triggered and if this happened within a certain time frame - ignore. I would hope there is a better way.

???
 
Very interesting, I never knew that happend with notepad. If this is the case, then maybe instead of letting them edit in the current directory, force the user to copy the file to thier PC. Then have them copy it back, this way you just check for an updated file, or newly created file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top