Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I need help using System.Web.HttpUtility.HtmlEncode in my VB.Net application 3 59
Get id from json Data with NewtonSoft 3 34
Host asp.net pages 5 25
Variable Event ? 3 23
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…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

20 Experts available now in Live!

Get 1:1 Help Now