Click here to Skip to main content
13,863,735 members
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 9 Jan 2009
Licenced CPOL

How to get the correct file time in C# in .NET Framework 2.0

, 9 Jan 2009
Rate this:
Please Sign up or sign in to vote.
Workaround to get the correct LocalDateTime of files no matter which date settings your computer has.


.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.


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:

  1. Create a file whose date-time is 11 September 2008, 11:51 PM
  2. Set your system date to 9 January 2009.
  3. Set your system time zone to GMT -2 Mid-Atlantic.
  4. 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...


  • 2008/01/09 - Initial report.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Argentina Argentina
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionThanks for the tip Pin
Dan Randolph31-Oct-14 12:56
professionalDan Randolph31-Oct-14 12:56 
GeneralIs localoffset needed Pin
vijay_kj8-Feb-10 0:42
membervijay_kj8-Feb-10 0:42 
GeneralOverloaded versions [modified] Pin
Alexandru Matei16-Mar-09 1:16
memberAlexandru Matei16-Mar-09 1:16 
AnswerRe: Overloaded versions Pin
pablodg16-Mar-09 1:49
memberpablodg16-Mar-09 1:49 
GeneralThank you Pin
Antonino Porcino18-Jan-09 23:26
memberAntonino Porcino18-Jan-09 23:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190214.1 | Last Updated 9 Jan 2009
Article Copyright 2009 by pablodg
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid