System.IO.FileSystemWatcher

  • Thread starter Thread starter John Talli
  • Start date Start date
J

John Talli

System.IO.FileSystemWatcher

When a user copies and pastes a file from one directory to a directory that is monitored
by the FileSystemWatcher, the FileSystemWatcher executes twice, once for a CREATED file
event and once for a CHANGED file event.

I think I understand why. First the monitored file is created and then contents are
inserted into it. (Although the file datetime remains the same.)

This is not what I require. If there is a new file created in a monitored directoy, I
only want TRUELY CREATED event raised, not the default of CREATED event raised & CHANGED
event raised.

Is there anyway I can turn-off the second event (CHANGED event raise) ?

Thanks
 
Hi this is a bug from Microsoft in this Obj

''' <remarks>Implementing a trick to stop double call from the onChange
FileSystemWatcher event bug</remarks>
Protected Sub OnChanged(ByVal source As Object, ByVal e As
FileSystemEventArgs)
Try

If lastChange <>
TimeSpan.FromTicks(Now.Ticks).TotalMilliseconds Then
'DO YOUR STUFF HERE
End If

lastChange =
TimeSpan.FromTicks(Now.Ticks).TotalMilliseconds

Catch ex As Exception
errorHandler(ex, Me.GetType.ToString & ".onChanged")

Finally
End Try

End Sub

Hope this help

Nicolas
 
Thanks!!! I'll play with this and let everyone know the outcome. This double call is
an interesting feature from MS.
 
Thanks again Nicolas.

It does appear MS is logging multiple events for a file copy. (ugh) Nicolas's
workaround of utilizing TimeSpan.FromTicks(Now.Ticks).TotalMilliseconds is wonderful
when monitoring a single directory. ( watchfolder.IncludeSubdirectories = False ).

But another problem popped-up when setting :
watchfolder.IncludeSubdirectories = True

Example of the "another" problem :
Folder Setup
C:\temp\FSW
C:\temp\FSW\FSW2

watchfolder.Path = "C:\temp\FSW"
watchfolder.IncludeSubdirectories = True


Now I copy & pasted a file into "C:\temp\FSW\FSW2" called "testDoc1.txt"
I would have expected Nicolas's solution to also work here. But no, MS has another
surprise in store.

The output of my log file shows:
#1 File C:\temp\FSW\FSW2\testDoc1.txt has been created by SYSTEM from OWNER-PC
#2 File C:\temp\FSW\FSW2 has been changed by SYSTEM from OWNER-PC
#3 File C:\temp\FSW\FSW2\testDoc1.txt has been changed by SYSTEM from OWNER-PC

Notice MS's surprise of #2. It is telling us that that the "Folder" has been modified
before continuing with #3.

If watchfolder.IncludeSubdirectories = False
the order is:
#1 File C:\temp\FSW\FSW2\testDoc1.txt has been created by SYSTEM from OWNER-PC
#2 File C:\temp\FSW\FSW2\testDoc1.txt has been changed by SYSTEM from OWNER-PC

The solution to watchfolder.IncludeSubdirectories = True
is to monitor for #2 File C:\temp\FSW\FSW2 has been changed by SYSTEM from OWNER-PC
which means a FOLDER was changed and just skip any processing for that.

To do this I had to increase the difference (CurrentTime - PreviousTime)
in Milliseconds = 400 to Milliseconds = 800.
 
Back
Top