none
FileSystemWatcher Deleted event RRS feed

  • Question

  • I have learned the hard way that deleting a file in Windows does not necessarily mean that the file gets deleted right away, it will just be marked for deletion until the last handle to the file is closed.

    When does FileSystemWatcher raise Deleted event?

    Tuesday, September 18, 2018 7:35 PM

All replies

  • Hi EuroEager,

    Thank you for posting here.

    For your question, please check the remarks in the MSDN document. It gives a good explanation.

    https://docs.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher.deleted?view=netframework-4.7.2

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 19, 2018 6:10 AM
    Moderator
  • I am sorry, but I did read the referred document prior to my post.

    I cannot see how the referred document explains the potential problem I described.

    Please be aware of: https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-deletefile and prticularly the line stating: "The DeleteFile function marks a file for deletion on close. Therefore, the file deletion does not occur until the last handle to the file is closed."

    Wednesday, September 19, 2018 7:45 AM
  • It's mean very simple thing - file can be opened by several program or even thread's in the same program. For each file opening there will be a handle in the system.

    File will remain on place until last handle will not be closed.

    There can be many operation DeleteFile.

    As far as I remember - notification will be send when file are actually deleted.


    Sincerely, Highly skilled coding monkey.

    Wednesday, September 19, 2018 8:15 AM
  • Thanks

    This was a kind of reply at least (even if you say "as far as I remember")

    If someone is sure, please tell me, I am aware of the possible problem, but I don't really know how to test it myself

    Wednesday, September 19, 2018 8:23 AM
  • >I don't really know how to test it myself

    Simple. 

    Make 3 application.

    1. & 2. - the same - open file for read in shared mode. They need to open file and wait to close file/handle for deletion. Can be on exit.

    3. - watcher.

    Run watcher. run two file handler and close them one by one - you will have a watcher reaction when file deleted.

    About details - sorry, didn't do anything with files for 8 years and with shared file much before first .Net was released.


    Sincerely, Highly skilled coding monkey.

    Wednesday, September 19, 2018 8:34 AM