MidnightBlueKnight
MidnightBlueKnight

Reputation: 21

Getting the Creation Date of a .txt C#

I need to get the date of a .txt file located somewhere in the C:\ directory.

I used ->

DateTime fileCreatedDate = File.GetCreationTime(@"C:\\docname.txt");

and wrote it to the console like this Console.WriteLine(fileCreatedDate.ToString())

what I got was 12/31/1600 7:00:00 PM which isn't possible because I created the document today. Can someone explain?

I am still learning how to get the creation date of a file and this was my first attempt but I am very curious to why this happened. Thanks!

Upvotes: 0

Views: 773

Answers (1)

K. L.
K. L.

Reputation: 261

You are getting the default time because the file does not exist.

As specified in the documentation:

If the file 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.

This can result in 12/31/1600 7:00:00 PM

Maybe add a check if the file exists before trying to retrieve the creation date.

Upvotes: 1

Related Questions