Why are FileSystemWatcher Attribute changes detected on Windows 7 but not Windows 8?

后端 未结 5 1066
太阳男子
太阳男子 2020-12-14 06:31

I have some code that uses FileSystemWatcher to monitor file changes outside of my application.

On Windows 7, using .NET 4, the below code would detect when a file

相关标签:
5条回答
  • 2020-12-14 06:34

    I don't know why but I find that under Windows 8.1

    NotifyFilters.LastWrite (Changed event) of class FileSystemWatcher will fire

    • if I monitor directory inside my desktop(C:\Users\[user]\Desktop).

    the event will not fire

    • if I monitor program files directory (C:\Program Files (x86))

    May be related to permission but I don't know how to config it, both condition are run under administrator

    0 讨论(0)
  • 2020-12-14 06:38

    I had the same problem. This class seems to work on my windows 8 computer:

    https://stackoverflow.com/a/23743268/637142

    Reason why I use that class is because it behaves the same on windows 7 and windows 8.

    0 讨论(0)
  • 2020-12-14 06:42

    There are too many comments everywhere, I will just add an answer to verify that you are aware of the following issues:

    • get filesystemwatcher events to occur on main UI thread (look for Hans Passant's answer)
    • WPF - Cannot change a GUI property inside the OnChanged method (fired from FileSystemWatcher)

    Apparently the problem is that the event is raised on a background thread, and you need to marshal the call back to the UI thread.

    I have experienced a lot of trouble with the FileSystemWatcher class, and decided not to use it as you can see me describe here: https://stackoverflow.com/a/22768610/129130 . It is possible, however, that the problems I experienced were due to thread synchronization issues and / or hardware issues.

    0 讨论(0)
  • 2020-12-14 06:43

    Check the archive bit on the file before/after you edit it. Your code is only searching for Attributes changes, so my guess is that Windows 7 is updating the Archive bit on the file, and windows 8 is not.

    0 讨论(0)
  • 2020-12-14 06:51

    FileSystemWatcher is notoriously unreliable. Try subscribing to all the events and see if the others fire. One thing that you could try is to use a timer to examine the file for changes at regular intervals, say once every two seconds, instead of using FileSystemWatcher.

    0 讨论(0)
提交回复
热议问题