Solved

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

Posted on 2010-11-27
5
1,527 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now