Click here to Skip to main content
6,295,667 members and growing! (14,889 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » Files     Beginner License: The Code Project Open License (CPOL)

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

By pablodg

Workaround to get the correct LocalDateTime of files no matter which date settings your computer has.
C# (C# 1.0, C# 2.0, C# 3.0), .NET (.NET 1.1, .NET 2.0), Visual Studio (VS2005, VS2008), Dev
Version:4 (See All)
Posted:9 Jan 2009
Views:5,909
Bookmarked:21 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
6 votes for this article.
Popularity: 3.67 Rating: 4.71 out of 5

1

2
1 vote, 16.7%
3

4
5 votes, 83.3%
5

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:

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

image003.jpg

image002.jpg

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.

License

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

About the Author

pablodg


Member

Location: Argentina Argentina

Other popular Files and Folders articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralOverloaded versions [modified] PinmemberAlexandru Matei1:16 16 Mar '09  
AnswerRe: Overloaded versions Pinmemberpablodg1:49 16 Mar '09  
GeneralThank you PinmemberAntonino Porcino23:26 18 Jan '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Jan 2009
Editor: Smitha Vijayan
Copyright 2009 by pablodg
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project