Modified FileSystemWatcher event (for "LastWrite") is unreliable

I am trying to get a notification when a file is updated on disk. I am interested in receiving these notifications as soon as a flash occurs, but it seems that FileSystemWatcher only sends an event when a stream opens or closes.

In the code below, I write to a file and re-flush the buffer on disk. However, FileSystemWatcher only notified me once at the beginning of the recording and once when they ended.

Is there any other way to get these notifications? Or should I use a survey for this?

The code:

class Program { static void Main(string[] args) { FileSystemWatcher watcher = new FileSystemWatcher(Environment.CurrentDirectory, "Test.txt"); watcher.Changed += watcher_Changed; watcher.EnableRaisingEvents = true; using(TextWriter writer = new StreamWriter("Test.txt")) { WriteData(writer); WriteData(writer); WriteData(writer); WriteData(writer); } Thread.Sleep(10000); } private static void WriteData(TextWriter writer) { writer.WriteLine("Hello!"); writer.Flush(); Console.WriteLine(DateTime.Now.ToString("T") + "] Wrote data"); Thread.Sleep(3000); } static void watcher_Changed(object sender, FileSystemEventArgs e) { Console.WriteLine(DateTime.Now.ToString("T") + "] Watcher changed!"); } } 

UPDATE 1

I adjusted the ToString function for DateTime to show seconds. Here is the result with the code above:

 11:37:47 AM] Watcher changed! 11:37:47 AM] Wrote data 11:37:50 AM] Wrote data 11:37:53 AM] Wrote data 11:37:56 AM] Wrote data 11:37:59 AM] Watcher changed! 

Thanks!

+4
source share
2 answers

This has nothing to do with FileSystemWatcher. The observer responds to updates to the LastWrite attribute for the file system.

eg. NTFS does not update LastWrite for each record. The value is cached and written only when the stream is closed or at some other indefinite time. This document says

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

I assume that similar caching is used for writing

+1
source

I think one of your problems here is that all your recordings are done before the event gets a piece of this processor. MSDN status

The Changed event occurs when changes are made to the size, system attributes, last record time, last access time or security permission of a file or directory in a controlled directory.

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

09:32] The observer has changed!

09:32] The observer has changed!

09:32] -----> Posted data

09:32] -----> Posted data

09:32] -----> Posted data

09:32] -----> Posted data

09:32] The observer has changed!

09:32] The observer has changed!

I assume that this type proves that even happens right after calling Flush (), it is just a matter of when the event handler is executed (and I assume that it also groups events). I do not know the specific needs of your project, but I would not question. Sounds like a waste, since FileSystemWatcher does what you want it to do in my opinion.

Edit: Well, I think my brain was not yet ready for thought when I posted it. Your conclusion that it fires when you open and close a stream seems more logical and correct. I guess I was looking for "prove" that it works when you call a flash, and therefore I found it - somehow.

Update I just pulled it out in the USN-Journal, and it seems that you will not get what you want, since it only records 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 may be interesting to read as well.

I also ran DiskMon to find out if it is receiving real-time changes. This is not so, but I do not know if it was intentional or not. However, the problem is that they require administrator rights to run. Therefore, I think you are stuck in FileSystemWatcher. (In any case, you need updates, this is not the way you can read a file while it is open / locked by another program.)

ps: I just noticed that you are a BugAid developer, which I only recently found out about - it looks amazing :)

+2
source

Source: https://habr.com/ru/post/1399725/


All Articles