Solved

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

Posted on 2010-11-27
5
1,580 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
  • 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 250 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 250 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MailAddress in vb 4 29
C# MVC Insert Multiple Row into DB 2 32
abstract class C# 1 31
Install IIS7.5 on Windows Sever 2012 R2 4 24
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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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