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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileSystemWatcher reacts twice 2

IlyaRabyy

Programmer
Nov 9, 2010
566
0
16
US
Colleagues,
Here's what I have in the code:

Code:
'====================================================================================================================================
Private Sub MonitorFilesModifications()
'====================================================================================================================================
goFW_XML.Filter = "*.XML"
goFW_XML.Path = gsWorkDir
goFW_XML.IncludeSubdirectories = True
goFW_XML.EnableRaisingEvents = True
goFW_XML.NotifyFilter = NotifyFilters.Attributes Or
								NotifyFilters.CreationTime Or
								NotifyFilters.DirectoryName Or
								NotifyFilters.FileName Or 
								NotifyFilters.LastWrite Or 
								NotifyFilters.Size

It works, but this FW reacts on any modification (like adding/removing chars and then Ctrl+S) twice:
The file modification's monitoring Proc looks like this:

Code:
'====================================================================================================================================
Private Sub OnChanged(sender As Object, e As FileSystemEventArgs)
'====================================================================================================================================
If e.ChangeType <> WatcherChangeTypes.Changed Then
   Return
End If

Dim lsReptStr As String = Format(Now, "yyyy-MM-dd HH:mm:ss") + ": File " + e.FullPath + " was modified on " + gcMachineName + vbCrLf
My.Computer.FileSystem.WriteAllText(gsLogFile, lsReptStr, True)

End Sub
'====================================================================================================================================

and this is what it writes into the LOG file:

Code:
2024-07-29 14:01:34: File C:\Temp\YE1099NEC24032901220240329151525.XML was modified on MYWS
2024-07-29 14:01:35: File C:\Temp\YE1099NEC24032901220240329151525.XML was modified on MYWS

Why it behaves like that, and what shall I do to prevent this duplication?

AHWBGA!

Regards,

Ilya
 
When I tried it, I also noticed that the OnChange event sometimes reacts twice, but I'm not sure if it's always like that, or if it only happens when the file is changed by Notepad (and not by another application).
This should be investigated and if it always happens twice then you could accept the first and ignore the second, or vice versa.
 
The way some applications work with files is to create a temporary copy, work with that, then when you save for example they delete the original file and copy the temp file to the original filename. So you will see two change notifications to the file. And some apps may generate 3! For example, see this article:
 
Code:
when the file is changed by Notepad
Actually, it's Notepad++, but might be the same.

Code:
create a temporary copy, work with that, then when you save for example they delete the original file and copy the temp file to the original filename
see this article: [URL unfurl="true"]https://devblogs.microsoft.com/oldnewthing/2014050...[/URL]
It looks like this is what happens.

Thank you both, colleagues!
I will play with the NotifyFilter and see what transpires...

Regards,

Ilya
 
After rigorous testing, it's been established that whatever the NotifyFilter(s) is/are - the notification is doubled.
To fight this discrepancy, I recalled that there is such thing as Static local memvar... and here it is:

Code:
' To prevent double notification:
Static lsPrevFile As String

If e.FullPath = lsPrevFile Then Return

Dim lsReptStr As String = Format(Now, "yyyy-MM-dd HH:mm:ss") + ": File " + e.FullPath + " was modified on " + gcMachineName + vbCrLf
My.Computer.FileSystem.WriteAllText(gsLogFile, lsReptStr, True)

Dim lsSubject As String = "ALERT: File's modified on " + gcMachineName
Dim lsEmailMsg As String = Format(Now, "yyyy-MM-dd HH:mm:ss") + ": File " + e.FullPath + " was modified on " + gcMachineName

Dim llEMailRet As Boolean

llEMailRet = SendEMail(lsEmailMsg, lsSubject, gsEMailAddressee, gsEMailSender, gcSMTPClient, giSMTPPort, gsLogFile, lsReptStr, gsEMailCC, gsEMailBCC)
lsPrevFile = e.FullPath

And this works, colleagues, it does work!

("What d'ya think of that?" Ian Gillan's ad-lib at the end of "Anyone's Daughter", Deep Purple, "Fireball", 1971 :) )

Thank you all for the help and tips!

Regards,

Ilya
 

Part and Inventory Search

Sponsor

Back
Top