Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-11-27
5
Medium Priority
?
1,683 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

610 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