![]() |
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.0By pablodgWorkaround 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
|
|
Advanced Search |
|
|
|
||||||||||||||||

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


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.
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;
}
Using GetExplorerFileDate, you can get the 'real' date of your files, even when the bug is not fixed...
| You must Sign In to use this message board. | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
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 |