probably at the beach
probably at the beach

Reputation: 15207

How can FileInfo.LastWriteTime be earlier than FileInfo.CreationTime?

I was debugging some code today and noticed one of my FileInfo objects had its LastWriteTime earlier than its CreationTime time. Is this possible/expected?

Upvotes: 11

Views: 9063

Answers (4)

David Heffernan
David Heffernan

Reputation: 612964

These properties of the file can be modified to take any value. So there's absolutely nothing to stop this happening.

That said, the most likely explanation is that the file was copied from one place to another. The creation time will be the time the copy took place. The modification time will be the time the source file was last modified, i.e. before the copy. So, when the file is copied, the modification time is also copied. To see this happen, simply pick a file on your machine. Create a copy of it (CTRL+C, CTRL+V) and look at the properties of the copy.

In other words, it is actually not a contradiction for the creation time to be later than the modification time. The creation time is when this file object was created. The modification time is when the file contents were modified.

Upvotes: 25

Habib
Habib

Reputation: 223257

Sure its possible, just copy some file and past it in the same folder, you will see the dates are different: enter image description here

Upvotes: 5

Alexander
Alexander

Reputation: 3247

This method may return an inaccurate value, because it uses native functions whose values may not be continuously updated by the operating system.

http://msdn.microsoft.com/en-us/library/system.io.filesysteminfo.lastwritetime.aspx

Upvotes: 2

Joey
Joey

Reputation: 354526

Of course it's possible. Creation and modification time of a file are just metadata and can be changed by anyone who feels like it.

Upvotes: 2

Related Questions