views:

89

answers:

4

I have a text file that several instances of the same app use to synchronize and exchange data between themselves. In a nutshell it's a plain text file that contains list of data files. These data files come and go on a regular basis.

Is there a reliable way to know that some app wrote to the file since it was last read from? Normally you would use Modified date for it. However the file is accessed so frequently that it will likely be constantly present in memory cache. So there is a flight chance that writing to the file will not alter Modification date.

Is there any other way to know that file was changed.

I know about file system notifications but would rather not use them here.

The software is in C# but obviously I can also use any WinAPI function via InteropServices. Software runs on Windows Server 2008 R2. File system is NTFS. Ideally the method should also work on network shares.

+1  A: 

May be better use sql data base for this. Otherwise you must organize a transaction mechanism for this file.

MaxFX
+1  A: 

How about using a hash (MD5 or SHA1 etc) of the file contents to determine if the file has been modified?

Shannon Cornish
A: 

Each time you access the file save its Hash (SHA2) and then in the future create a Hash again and compare it to what was saved previously.

Simple and elegant.

Sergio Tapia
+2  A: 

One interesing but (AFAI) barely used feature of ntfs is it's USN Journal, which you can read. It maintaines a list of all changes to the filesystem. The only program I know which explicitly uses it is Everything. But it might be that the FileSystemWatcher is using it internally. Big advantage of reading this log manually is that you can figure out changes that happend while your program was not running.

Mene