Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creation Date and File.Copy Issue

Tags:

c#

.net

file-io

I am trying to copy files from one directory to another and test based upon the file creation date.

File.Copy(fileName, directory + fileNameOnly, true);

The problem occurs later in my program when I checked the creation date to ensure it is no more than 5 days old.

FileInfo file = new FileInfo(fileName);
if (file.CreationTime.AddHours(120) < DateTime.Now) {}

I have seen that the creation date when copied back is set to 1980-01-01. This is not useful for my requirements as I would like maintain the creation date from the original file. Is there another method of comparing the dates or is it the copy that loses the creation date value.

I guess my question is, how can I maintain the Creation Date?

like image 345
Mr. Mr. Avatar asked Dec 15 '10 17:12

Mr. Mr.


People also ask

Does copying a file change creation time?

Windows does not preserve the creation_time in the copied file of the original file. The modification time is copied. The creation time is always the current system time.

Does copying a file change the date modified?

If you copy a file from C:\fat16 to D:\NTFS, it keeps the same modified date and time but changes the created date and time to the current date and time. If you move a file from C:\fat16 to D:\NTFS, it keeps the same modified date and time and keeps the same created date and time.

How do I find the original creation date of a file?

You can show the "creation date" that is stored for files in the Windows Explorer easily: Switch Windows Explorer to column view, right click a column header and and in the context menu that pops up select "Creation Date" for enabling the additional column.


1 Answers

Use the File.SetCreationTime method after you copy the file.
You can get the source file's creation time with File.GetCreationTime

like image 198
Brian R. Bondy Avatar answered Oct 24 '22 06:10

Brian R. Bondy