FileInfo.LastWriteTime discrepancies [Lakshan Fernando]

I recently investigated a customer issue of incorrect time reporting with FileInfo.LastWriteTime and thought of sharing this information in the blog. In the customer case, the last write time was off by about one hour for some files.

 

We depend on two Win32 APIs to get the file time information, GetFileAttributesEx in the normal case and FindFirstFile if there is a problem with the call to GetFileAttributesEx. One case where we will call FindFirstFile is if there is already an open handle to the specific file. As File Times section on MSDN describes, when writing to a file, the last write time is not fully updated until all handles that are used for writing are closed. This is the most likely reason that the customer saw the last write time reported incorrectly..