?
Solved

.net 2003 C Sharp System.IO.FileInfo.LastWriteTime is wrong

Posted on 2010-11-27
5
Medium Priority
?
1,644 Views
Last Modified: 2012-05-10
I have C sharp code that loops through directories and returns a file list. Then I use System.IO.FileInfo.LastWriteTime and System.IO.FileInfo.fi.CreationTime to get the modified and created date. It seems like it worked ok until I verified the dates against Windows Explorer and found that they were an hour off on some files, but ok on others. I read up and tried a fix here on Experts Exchange and it made some files two hours off. I double checked my current timezone and time on my computer and it is ok. If I use VBScript, it is exact. I don't see why this simple task should be so difficult. Any help is much appreciated.
//TEST FILE THAT DIFFERS FROM EXPLORER
System.IO.FileInfo fi = new System.IO.FileInfo("c:\windows\notepad.exe");

//OUTPUT IS AN HOUR OFF
Console.WriteLine("Date Modified: " + fi.LastWriteTime

//OUTPUT IS TWO HOURS OFF
Console.WriteLine("Date Modified: " + fi.LastWriteTime.Add(System.TimeZone.CurrentTimeZone.GetDaylightChanges(fi.LastWriteTime.Year).Delta));

Open in new window

0
Comment
Question by:advcom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:BurnieP
ID: 34225551
Yes, those properties are not 100% accurate.  You can try using the Refresh() method first.

fi.Refresh();

See this post : http://msdn.microsoft.com/en-us/library/system.io.filesysteminfo.lastwritetime.aspx

Remarks
--------------------------------------------------------------------------------

Note   This method may return an inaccurate value, because it uses native functions whose values may not be continuously updated by the operating system.

The value of the LastWriteTime property is pre-cached if the current instance of the FileSystemInfo object was returned from any of the following DirectoryInfo methods:

GetDirectories
GetFiles
GetFileSystemInfos
EnumerateDirectories
EnumerateFiles
EnumerateFileSystemInfos

To get the latest value, call the Refresh method.

If the file described in the FileSystemInfo object does not exist, this property will return 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adjusted to local time.

This property value is null if the file system containing the file does not support this information.

0
 

Author Comment

by:advcom
ID: 34226078
hmm, guess that was not the answer I wanted to hear. I put the fi.refresh() into my code and get the same inaccurate results. I really cannot except an approximate modified and created datestamp. Are you aware of a workaround? I will see what alternatives I can find.
0
 
LVL 16

Accepted Solution

by:
BurnieP earned 1000 total points
ID: 34226095
I found something else at :

http://www.codeproject.com/KB/files/csharpfiledate.aspx

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:

 Collapse | Copy Code
static DateTime GetExplorerFileDate(string filename)
{
    DateTime now = DateTime.Now;
    TimeSpan localOffset = now - now.ToUniversalTime();
    return File.GetLastWriteTimeUtc(filename) + localOffset;
}

You can try using that function and see if you get better results?
0
 
LVL 4

Assisted Solution

by:ricovox
ricovox earned 1000 total points
ID: 34230934
Hi,

Yes I agree with BurnieP. This problem is caused by issues with the way windows interprets "Daylight Savings Time" (DST).

I would use the following functions (similar to BurnieP's)

            static DateTime GetExplorerFileDate(string fileFullPath)
            {
                TimeSpan localOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
                return (new FileInfo(fileFullPath)).LastWriteTimeUtc + localOffset;
            }

            static DateTime GetExplorerFileDate(FileInfo fi)
            {
                TimeSpan localOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
                return fi.LastWriteTimeUtc + localOffset;
            }

Open in new window

0
 

Author Comment

by:advcom
ID: 34688878
I gave up on the project. I need accurate date stamps. I could not get the suggested functions to work. It may have been my fault, I do not normally program in C#. I decided to move on to other things, so I will award the points to burniep for the effort.
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question