.NET FileInfo.LastWriteTime & FileInfo.LastAccessTime are wrong

The FileInfo values are only loaded once and then cached. To get the current value, call Refresh() before getting a property:

f.Refresh();
t = f.LastAccessTime;

Another way to get the current value is by using the static methods on the File class:

t = File.GetLastAccessTime(path);

Starting in Windows Vista, last access time is not updated by default. This is to improve file system performance. You can find details here:

http://blogs.technet.com/b/filecab/archive/2006/11/07/disabling-last-access-time-in-windows-vista-to-improve-ntfs-performance.aspx

To reenable last access time on the computer, you can run the following command:

fsutil behavior set disablelastaccess 0


As James has pointed out LastAccessTime is not updated.


The LastWriteTime has also undergone a twist since Vista. When the process has the file still open and another process checks the LastWriteTime it will not see the new write time for a long time -- until the process has closed the file.

As a workaround you can open and close the file from your external process. After you have done that you can try to read the LastWriteTime again which is then the up to date value.


File System Tunneling:

If an application implements something like a rolling logger which closes the file and then renames it to a different file name you will also run into issues since the creation time and file size of the "old" file is remembered by the OS although you did create a new file. This includes wrong reports of the file size even if you did recreate log.txt from scratch which is still 0 bytes in size. This feature is called OS File System Tunneling which is still present on Windows 8.1 . An example how to work around this issue check out RollingFlatFileTracelistener from Enterprise Library.

You can see the effects of file system tunneling on your own machine from the cmd shell.

echo test > file1.txt
ren file1.txt file2.txt
Wait one minute
echo test > file1.txt

dir  /tc file*.txt
...
05.07.2015  19:26                 7 file1.txt
05.07.2015  19:26                 7 file2.txt

The file system is a state machine. Keeping states correctly synchronized is hard if you care about performance and correctness.

This strange tunneling syndrome is obviously still used by application which do e.g. autosave a file and move it to a save location and then recreate the file again at the same location. For these applications it makes to sense to give the file a new creation date because it was only copied around. Some installers do also such tricks to move files temporarily to a different location and write the contents back later to get past some file exists check for some install hooks.


Have you tried calling Refresh() just before accessing the property (to avoid getting a cached value)? If that doesn't work, have you looked at what Explorer shows at the same time? If Explorer is showing the wrong information, then it's probably something you can't really address - it might be that the information is only updated when the file handle is closed, for example.


There is a setting in windows which is sometimes set especially on server systems so that modified and accessed times for files are not set for better performance.