FileSystemWatcher.Changed event gets triggered when shared excel file is closed

68 Views Asked by At

I'm using the FileSystemWatcher.Changed event in my VB.NET application to track changes in files, mainly Excel files. I have used NotifyFilters.LastWrite Or NotifyFilters.Attributes.

I have observed that the Changed event gets triggered when shared Excel files are closed, which does not happen in the case of non-shared Excel files.

I wanted to know if there is any specific reason behind this behavior and if there is a way to check if the Changed event got triggered because of closing the file. I have checked other forums and did not find any relevant cases and solutions. Also, I wanted to know if there is a way to trigger the Changed event only when changes are made to the Excel files or when the Excel files are saved.

' Sample code:
Sub New()
    Dim watcher As New FileSystemWatcher
    watcher.Path = "E:\FSW"
    watcher.NotifyFilter = NotifyFilters.LastWrite Or NotifyFilters.Attributes
    watcher.Filter = "*.*"
    AddHandler watcher.Changed, AddressOf File_Changed
    watcher.EnableRaisingEvents = True
End Sub
Private Sub File_Changed(ByVal source As Object, ByVal e As FileSystemEventArgs)
    Console.WriteLine($"Changed event occured Name: {e.Name}, FullPath:{e.FullPath}, ChangeType:{e.ChangeType}")
End Sub
0

There are 0 best solutions below