Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get the creation time of a file in C#
To get the creation time of a file in C#, you can use the CreationTime property of the FileInfo class or the static methods from the File class. This allows you to retrieve when a file was originally created on the file system.
Syntax
Using the FileInfo class −
FileInfo fileInfo = new FileInfo("filename.txt");
DateTime creationTime = fileInfo.CreationTime;
Using the static File class −
DateTime creationTime = File.GetCreationTime("filename.txt");
Using FileInfo Class
The FileInfo class provides an object-oriented approach to work with file information. Create a FileInfo object and access its CreationTime property −
using System.IO;
using System;
public class Program {
public static void Main() {
using (StreamWriter sw = new StreamWriter("qa.txt")) {
sw.WriteLine("Questions and Answers!");
}
FileInfo file = new FileInfo("qa.txt");
DateTime dt = file.CreationTime;
Console.WriteLine("File creation time: " + dt);
}
}
The output of the above code is −
File creation time: 9/5/2018 5:20:03 AM
Using File.GetCreationTime() Method
The static File.GetCreationTime() method provides a simpler approach without creating a FileInfo object −
using System.IO;
using System;
public class Program {
public static void Main() {
using (StreamWriter sw = new StreamWriter("sample.txt")) {
sw.WriteLine("Sample file content");
}
DateTime creationTime = File.GetCreationTime("sample.txt");
Console.WriteLine("Creation time: " + creationTime);
Console.WriteLine("Creation date only: " + creationTime.ToShortDateString());
Console.WriteLine("Creation time only: " + creationTime.ToShortTimeString());
}
}
The output of the above code is −
Creation time: 9/5/2018 5:20:03 AM Creation date only: 9/5/2018 Creation time only: 5:20 AM
Using CreationTimeUtc for UTC Time
For getting the creation time in Coordinated Universal Time (UTC), use CreationTimeUtc property or File.GetCreationTimeUtc() method −
using System.IO;
using System;
public class Program {
public static void Main() {
using (StreamWriter sw = new StreamWriter("utc_test.txt")) {
sw.WriteLine("UTC time test");
}
FileInfo file = new FileInfo("utc_test.txt");
Console.WriteLine("Local time: " + file.CreationTime);
Console.WriteLine("UTC time: " + file.CreationTimeUtc);
// Using static method
DateTime utcTime = File.GetCreationTimeUtc("utc_test.txt");
Console.WriteLine("UTC via File class: " + utcTime);
}
}
The output of the above code is −
Local time: 9/5/2018 5:20:03 AM UTC time: 9/5/2018 10:20:03 AM UTC via File class: 9/5/2018 10:20:03 AM
Comparison
| Method | Use Case | Performance |
|---|---|---|
| FileInfo.CreationTime | When working with multiple file properties | Better for multiple operations on same file |
| File.GetCreationTime() | Quick one-time retrieval | More efficient for single operations |
| CreationTimeUtc | When UTC time is required | Same as local time methods |
Conclusion
Getting file creation time in C# can be accomplished using either the FileInfo class properties or the static File class methods. Choose FileInfo when working with multiple file attributes, or use File.GetCreationTime() for simple, one-time retrieval of creation timestamps.
