Results 1 to 6 of 6

Thread: FileSystemWatcher.Changed event get triggred when shared excel file gets closed

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2020
    Posts
    25

    FileSystemWatcher.Changed event get triggred when shared excel file gets closed

    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
    Last edited by IT_Researcher; Sep 17th, 2023 at 11:39 PM.

  2. #2
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,368

    Re: FileSystemWatcher.Changed event get triggred when shared excel file gets closed

    If (and from your description it sounds like that) this is common excel behavior, then there is probably nothing you can do about it.
    It makes sense that excel writes the Share information (Who has the workbook open now) to the file and removes it when the shared file is closed.
    For such files, you would need to further examine the file when FileSystemWatcher.Changed fires to determine if there really was a change.

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,687

    Re: FileSystemWatcher.Changed event get triggred when shared excel file gets closed

    Just so we are on the same page, what do you mean by, "shared Excel files"?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,284

    Re: FileSystemWatcher.Changed event get triggred when shared excel file gets closed

    He meant Excel files in a shared network folder. A lock file is created in the same folder when an Excel file is opened so that if other users try to open it, it only opens in read-only mode. When the initial users close the file, the lock file is deleted automatically.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,896

    Re: FileSystemWatcher.Changed event get triggred when shared excel file gets closed

    Quote Originally Posted by stanav View Post
    He meant Excel files in a shared network folder. A lock file is created in the same folder when an Excel file is opened so that if other users try to open it, it only opens in read-only mode. When the initial users close the file, the lock file is deleted automatically.
    Unlikely. Excel has an option to create a shared workbook that allows multiple people to have it open at the same time, each one making their own changes to it. In older versions of Excel, this was enabled through the Tools->Share Workbook menu item. You could then configure things like when updates made by others become visible to you, or how to handle cases where you and another user modify the same field(s). I would bet an enormous amount of money that is what the OP is referring to.

    Edit to add: And it is probably the behind the scenes file level stuff that allows Excel to function with multiple people accessing the same file at the same time that is causing the FileChanged event to trigger, since I'm sure that Excel is storing information inside the file about who else has it open, and that needs to be updated when someone closes the workbook, even if they didn't make manual changes to it.
    Last edited by OptionBase1; Oct 6th, 2023 at 04:13 PM.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,624

    Re: FileSystemWatcher.Changed event get triggred when shared excel file gets closed

    I suspect it's the truly shared file, as well. Though it's mighty nice to see Stanav drop by

    Co-editing has become so common for spreadsheets, perhaps starting with Google Docs, that MS pretty much had to add the functionality into Excel. Most likely the file really IS changing. After all, an xlsx isn't really a file at all. It's really a zipped directory tree. What is in that tree? Well, there's quite a lot, really. Most likely one of the directories holds a bunch of information that facilitates the sharing. That directory may not even exist, or may not be used, for single use. That's just a guess, though, I've never really wandered around in that directory tree much, just looked at the size of the thing, checked out a couple files...then fled.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width