How to Set File Creation Time in C#

The GetCreationTime property of the File class returns a DateTime object, the date and time when a file was created. The datetime includes the full date and time. The SetCreationTime property of the File class sets the creation time of a file.

The following code snippet returns the creation time of a file and displays it on the console. The code also overrides the original creation time with the current date and time, reads it back, and displays it to the console. 

string textFile = @"C:\Mahesh\Data\Authors.txt";  
if (File.Exists(textFile))  
{  
// Read oritinal file creation time  
DateTime creationTime = File.GetCreationTime(textFile);  
// Display creation time  
Console.WriteLine(creationTime.ToString("MM/dd/yyyy HH:mm:ss"));  
// Manually override previous creation time to now  
File.SetCreationTime(textFile, DateTime.Now );  
Console.WriteLine(File.GetCreationTime(textFile));  
}

Make sure to replace the file's full path with an existing file on your system.


Similar Articles