GetLastWriteTime returning 12/31/1600 7:00:00 PM
Asked Answered
D

6

18

I am using the following code to write the Date Modified time of a Directory to a label

string selectedPath = comboBox1.SelectedItem.ToString();
DateTime lastdate = Directory.GetLastWriteTime(selectedPath);
datemodified.Text = lastdate.ToString();

It returns the date 12/31/1600 7:00:00 PM which I have no clue where it is getting that date from. Can anyone help me understand why it is returning that date and how I can fix it? I'm using .NET 3.5

Demilune answered 14/5, 2012 at 13:6 Comment(0)
F
43

From the documentation:

If the directory described in the path parameter does not exist, this method returns 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adjusted to local time.

So presumably your time zone is UTC-5 (in January), and the directory doesn't exist...

Fractional answered 14/5, 2012 at 13:8 Comment(6)
Just tell me Jon where do you remember all that?Ljubljana
@NikhilAgrawal by looking up the definition of the call on msdn. A person doesn't have to remember everything, just has to remember were to lookGeomancer
Is there any significance to that date (other than the parameter does not exist)?Arnie
@Snoop: It's probably just an epoch that happened to be chosen. There are lots of epochs in use throughout computing - 1582 for GUID timestamps, 1AD for .NET, 1970-01-01 for anything Unix-based, 1900 for Excel IIRC...Fractional
@JonSkeet Ok. I haven't done Java in a while, but I know they used to have one... which was like "when Java was invented" or something... I was wondering if there was historical significance behind Microsoft choosing that particular date.Arnie
@Snoop: No, Java's epoch is the Unix epoch. 1600 is probably "old enough to cope with most historical events that users would care about the timing of". (Although for a file system, who knows.)Fractional
T
2

An easy way to test for file not found with the result of GetLastWriteTime()/GetLastWriteTimeUtc() without hardcoding the sentinel epoch date/times that are used to indicate a file/dir not found condition, is as follows:

// ##### Local file time version #####
DateTime fileTimeEpochLocal=DateTime.FromFileTime(0);
// Use File.GetLastWriteTime(pathname) for files
// and Directory.GetLastWriteTime(pathname) for directories
DateTime lastWriteTime=Directory.GetLastWriteTime(selectedPath); 

// Check for a valid last write time
if (lastWriteTime!=fileTimeEpochLocal) // File found
    DoSomethingWith(selectedPath,lastWriteTime);
else // File not found
    HandleFileNotFound(selectedPath);

// ##### UTC file time version #####
DateTime fileTimeEpochUtc=DateTime.FromFileTimeUtc(0);
// Use File.GetLastWriteTimeUtc(pathname) for files
// and Directory.GetLastWriteTimeUtc(pathname) for directories
DateTime lastWriteTimeUtc=Directory.GetLastWriteTimeUtc(selectedPath);

// Check for a valid last write time
if (lastWriteTimeUtc!=fileTimeEpochUtc) // File found
    DoSomethingWith(selectedPath,lastWriteTimeUtc);
else // File not found
    HandleFileNotFound(selectedPath);
Thrall answered 14/1, 2019 at 19:20 Comment(0)
G
0

first thought is that of is your time set correctly. Second thought is to right click on that folder and see what it says in properties. Lastly I'd make new test folder and run that bit of GetLastWriteTime tests on it so you know what you are getting back.

Geomancer answered 14/5, 2012 at 13:10 Comment(1)
Monday, ‎May ‎07, ‎2012, ‏‎4:06:00 PM is the date on one of the folders. Its just strange to me. The code makes sense, but what its returning doesntDemilune
X
0

GetLastWriteTime not always return reliable date time, use this

string selectedPath = comboBox1.SelectedItem.ToString();
DateTime now = DateTime.Now;
TimeSpan localOffset = now - now.ToUniversalTime();
DateTime lastdate = File.GetLastWriteTimeUtc(selectedPath) + localOffset;
datemodified.Text = lastdate.ToString();
Xeres answered 5/5, 2016 at 17:30 Comment(0)
U
0

Old question, but today I faced this issue. That particular date is also returned when your path is invalid or the file doesn't exists, because there is no built in exception in any of those cases.

Ureide answered 27/7, 2016 at 13:6 Comment(0)
M
0

In .net core, you will need to get the absolute path of the file. Add reference to Microsoft.Extensions.Hosting and inject that into your constructor. The ContentRootPath property will be your web root.

Grab your server path

var Files = FIO.Directory.GetFiles("Unzipped");

This will be your actual path

var Path = string.Format(@"{0}\{1}",WebRootPath, Files[0]);

var CreationDate = File.GetLastWriteTime(Path);
Mayers answered 1/12, 2020 at 17:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.