Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2005

Tip: Looking for answers? Try searching our database.

Getting the Modified Time Stamp from a File in .Net

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ueslei - 08 Jul 2005 15:24 GMT
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
Jani Järvinen [MVP] - 09 Jul 2005 06:09 GMT
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.

Signature

Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
janij@removethis.dystopia.fi
http://www.saunalahti.fi/janij/

Morten Wennevik - 10 Jul 2005 09:38 GMT
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.

> Good day!
>
[quoted text clipped - 23 lines]
> But then again, FileInfo.LastWriteTime should always be the same as what
> Windows Explorer reports.

Signature

Happy coding!
Morten Wennevik [C# MVP]

Jani Järvinen [MVP] - 10 Jul 2005 12:15 GMT
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.

Signature

Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
janij@removethis.dystopia.fi
http://www.saunalahti.fi/janij/

Morten Wennevik - 10 Jul 2005 14:24 GMT
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.

> Hi Morten!
>
[quoted text clipped - 11 lines]
> Any ideas? If you want, we can continue discussing this of in the private
> newsgroups.

Signature

Happy coding!
Morten Wennevik [C# MVP]


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.