Getting the Modified Time Stamp from a File in .Net

  • Thread starter Thread starter Ueslei
  • Start date Start date
U

Ueslei

Hi!
I'm trying to get the Modified Time Stamp (that appears in Windows
Explorer).
I'm using:
fl.LastWriteTime.ToShortDateString();
But, in this case, if I've moved the file, it returns other date than
Windows Explorer.

Any suggestions?
HSH,
Ueslei
 
Good day!
I'm trying to get the Modified Time Stamp (that appears in Windows
Explorer).

You should be able to use the System.IO.FileInfo.LastWriteTime (or
LastWriteTimeUtc) property to read the same date/time value that Windows
Explorer shows in its "Date Modified" column. The other date/time properties
published are CreationTime and LastAccessTime, equivalents of which can be
seen in Windows Explorer when you open properties of any file.
But, in this case, if I've moved the file, it returns other date than
Windows Explorer.

When moving or copying files, the LastWriteTime property should stay the
same, and I cannot think of a situation immediately where this wouldn't be
the case.

However, different file systems store dates and times with a different
precision. For example, the FAT and NTFS file systems are different, as
could be file systems on network file servers. Also note that if you are
accessing a file from a network drive, the last modified time of the file
usually comes from the server. So if the clocks on the server and your PC
are out of sync, then you could see different times.

But then again, FileInfo.LastWriteTime should always be the same as what
Windows Explorer reports.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
I can confirm this problem, tried moving and/or renaming the files and lastwritetime in my case got reset to DateTime 0 (1.1.1601) This is not the real lastwritetime as Ueslei said, Windows Explorer displays the correct time.
 
Hi Morten!
I can confirm this problem, tried moving and/or renaming
the files and lastwritetime in my case got reset to DateTime 0.

I see -- haven't run into this problem myself. Instead, in those apps I've
been using it, LastWriteTime has been reliable, even if the files have been
moved and/or copied.

Could this be a bug in the framework version 1.1? Or a security issue of
somekind, where .NET cannot read the datatime info? I have yet to convert my
apps to .NET 2.0, so I'm not sure how the code works there.

Any ideas? If you want, we can continue discussing this of in the private
newsgroups.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
Hi Jani,

The problem seems to be fixed in Framework 2.0.

Upon closer inspection it appears that the 1.1 version of FileInfo returns a 'null' date if the file is not found.

I created a simple

FileInfo fi = new FileInfo("a.dat");
MessageBox.Show(fi.LastWriteTime.ToShortDateString());

Result is the correct date.

Changed the filename in windows explorer to a1.dat and ran the exact same code. I forgot to change the code to create a FileInfo for a1.dat since it had been renamed, but the code ran successfully and returned January 1st 1601. The 2.0 version recognized that there was no a.dat and gave an exception.
 
Back
Top