Introduction
.NET Framework 2.0 misreports file date-time information. Through the Time
and TimeInfo
functions, you may get a wrong time info for files. A workaround is provided here, so are instructions to reproduce the funny behaviour.
Background
Some guy reported this issue to MS, but the topic was dismissed because they were unable to reproduce it (C# LastWriteTime BUG).
To reproduce the problem, you may set the following conditions:
- Create a file whose date-time is 11 September 2008, 11:51 PM
- Set your system date to 9 January 2009.
- Set your system time zone to GMT -2 Mid-Atlantic.
- Set your system time to "Automatically adjust clock for daylight saving changes".
Step 3 might not be necessary.
Then, just by doing Console.Write(File.GetLastWriteTime(filename).ToString()));
, you will get a wrong date:
9/12/2008 12:51:43AM
If you disable "Automatically adjust clock for daylight saving changes", the reported date turns to be the correct one: 9/11/2008 11:51:43PM. In both cases, Windows Explorer reports 11:51:43PM.
Using the code
Following is a function to get the correct system date-time information:
static DateTime GetExplorerFileDate(string filename)
{
DateTime now = DateTime.Now;
TimeSpan localOffset = now - now.ToUniversalTime();
return File.GetLastWriteTimeUtc(filename) + localOffset;
}
Points of interest
Using GetExplorerFileDate
, you can get the 'real' date of your files, even when the bug is not fixed...
History
- 2008/01/09 - Initial report.