File.GetLastWriteTime seems to be returning 'out of date' value

Solution 1:

During my experience I went throw a couple of issues like yours. On Windows Vista/7 systems that function not always returns a reliable result.

After a while we found this link: Disabling Last Access Time in Windows Vista to improve NTFS performance

An observant Windows Vista user noticed a registry named NtfsDisableLastAccessUpdate under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ControlFileSystem and asked us what this means.

Last Access Time is a file attribute that’s updated when a file is accessed or otherwise touched. (This is often confused with the Last Modified Time, which is only updated when the file changes.) Last Access Time has a loose granularity that only guarantees that the time is accurate to within one hour.

In Windows Vista, we’ve disabled updates to Last Access Time to improve NTFS performance. If you are using an application that relies on this value, you can enable it using the following command:

fsutil behavior set disablelastaccess 0

You must restart the computer for this change to take effect. For more information about the Fsutil command and Last Access Time, see the Fsutil online Help.

Based on this it became clear that last access time can not be used as a "strong key". To resolve that issue, we just stop relaying on GetLastWriteTime call, but store last changed value of the file or in its name, like "FileName_yyyymmdd", or inside that file in some field.

There is another solution for GetLastAccessTime can find here:

.NET FileInfo.LastWriteTime & FileInfo.LastAccessTime are wrong, could be useful in your case too.

My general opinion on this would be: do not relay on that parameter, but invent something else in your architecture.

Good luck

Solution 2:

Tigran is right:

You can try to compare file size changes, in addition to last write time. It's what I do (with FileSystemWatcher, but it's similar to compare fields within a time window).