FileSystemWatcher changed event (for “LastWrite”) is unreliable

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 22:26:19

It has nothing to do with the FileSystemWatcher. The watcher reacts to updates on the LastWrite attribute for the filesystem.

E.g. NTFS does not update the LastWrite on every write. The value is cached and only written when the stream is closed or at some other unspecified time. This document says

Timestamps are updated at various times and for various reasons. The only guarantee about a file timestamp is that the file time is correctly reflected when the handle that makes the change is closed. [...] The NTFS file system delays updates to the last access time for a file by up to 1 hour after the last access

I assume a similar caching applies for write

I think one of your issues here is that all your writes get executed before the event gets a slice of that cpu-time. MSDN states

The Changed event is raised when changes are made to the size, system attributes, last write time, last access time, or security permissions of a file or directory in the directory being monitored.

I did a test and inserted Sleeps after each call to WriteData(...), what i got was

09:32] Watcher changed!

09:32] Watcher changed!

09:32] -----> Wrote data

09:32] -----> Wrote data

09:32] -----> Wrote data

09:32] -----> Wrote data

09:32] Watcher changed!

09:32] Watcher changed!

I guess this kind of proves that the even is fired right after you call Flush(), it's just a question of when the event-handler gets executed (and I assume it groups events as well). I don't know the specific needs of your project, but I wouldn't poll. Seems like a waste since FileSystemWatcher does what you want it to do in my opinion.

Edit: Ok, I guess my brain wasn't quite ready yet for thinking when I posted this. Your conclusion that it fires when you open and close the stream seems more logical and right. I guess I was looking for "prove" that it fires when you call flush and therefore found it - somehow.

Update I just had a poke at the USN-Journal and it seems that wont get you what you want either as it writes the record only when the file is closed. -> http://msdn.microsoft.com/en-us/library/aa363803(VS.85).aspx I also found a USN-Viewer in C# and http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/c1550294-d121-4511-ac32-31551497f64e might be interesting to read as well.

I also ran DiskMon to see if it gets the changes in realtime. It doesn't, but I don't know if that's intentional or not. However, the problem with both is that they require admin-rights to run. So I guess you are stuck with FileSystemWatcher. (What is it you need the updates for anyway, it's not like you can read the file while it's open/locked by another program.)

ps: I just noticed you are a dev of BugAid, which I was made aware of it only recently - it looks awesome :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!