I'm watching changes in a folder using ReadDirectoryChangesW (http://msdn.microsoft.com/en-us/library/aa365465%28VS.85%29.aspx
) with the FILE_NOTIFY_CHANGE_LAST_WR
When a file is modified my thread gets a FILE_NOTIFY_INFORMATION structure.
All works fine so far.
The problem: The event is sent several times, and I cannot distinguish between a file write in progress and a file write complete.
I watch the folder c:\temp
If I copy a file called testfile.txt (100 mb) with Windows Explorer to c:\temp
I get the FILE_NOTIFY_INFORMATION 3 (!!) times in a row. 2 times in the moment the write operation starts and a third (!) time when the copy is completed after 5 seconds.
If I copy a file called testfile.txt (100 mb) with Total Commander (a file manager) to c:\temp
I get the FILE_NOTIFY_INFORMATION 2t(!!) times in a row. First time in the moment the write operation starts and a second (!) time when the copy is completed after 5 seconds.
I want to know when the copy operation is completed, I have no interest in events when the copy operation starts.
How to distinguish it?
I cannot count the number of FILE_NOTIFY_INFORMATION events, since I get one to three events with differnet programs making the file changes (e.g. one with drop box, two with total commander, three with windows explorer).
I've tried this as a solution:
I use GetFileSecurityW after an event to check if I have GENERIC_WRITE access rights to the file. The problem is that as administrator windows reports that I have write rights even during the copy operation, not only after it has been completed.
My code is in Delphi, but a solution in C++ will be also accepted.