In C#, File.SetCreationTime() changes the creation time that the file system stores for a file. You pass a DateTime that is read as local time, the same zone that GetCreationTime shows.
Only the timestamp changes; the content of the file is not touched. The file must already exist. Setting a fixed time is the reliable way to test code that depends on file age, because the real time keeps moving. The UTC form is SetCreationTimeUtc.
This is a static method of System.IO.File. Call it as File.SetCreationTime(...); there is no new File(). For an object that remembers one file, see FileInfo.
Syntax
Pass the path of an existing file and the new DateTime.
File.SetCreationTime(path, new DateTime(2020, 1, 1, 0, 0, 0))
Gives the file a fixed old date, handy for testing.
FileInfo.CreationTime is a property you can also assign. The static call needs only the path, which is handy for one quick change.
Library declarations
C#
public static void SetCreationTime(string path, DateTime creationTime);
Parameters
Parameter
Description
path
The file to change. It must exist.
creationTime
The new creation time. Treated as local time unless its Kind says otherwise.
Return Value
Return Value
Description
void
Nothing is returned. Read the result back with GetCreationTime.
C# Examples
Example 1 — Set a Creation Time and Read It Back
Give a file a fixed local creation time and confirm it with GetCreationTime.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-setcreationtime");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "stamp.txt");
File.WriteAllText(path, "demo");
DateTime stamp = new DateTime(2026, 1, 15, 9, 30, 0);
File.SetCreationTime(path, stamp);
Console.WriteLine(File.GetCreationTime(path) == stamp);
Console.WriteLine(File.GetCreationTime(path).ToString("yyyy-MM-dd"));
Directory.Delete(root, true);
}
}
Output
True
2026-01-15
How It Works
Build the new time.new DateTime(2026, 1, 15, 9, 30, 0) is 9:30 on 15 January 2026, with no seconds.
One call changes it.SetCreationTime replaces the creation time. The file content demo stays as it was.
It reads back equal. The comparison with == prints True, and the date text shows the day that was set.
Example 2 — An Unspecified DateTime Is Treated as Local
See what Kind the value has, and check it against the UTC view.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-setcreationtime-kind");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "kind.txt");
File.WriteAllText(path, "demo");
DateTime stamp = new DateTime(2026, 1, 15, 9, 30, 0);
Console.WriteLine(stamp.Kind);
File.SetCreationTime(path, stamp);
Console.WriteLine(File.GetCreationTimeUtc(path) == stamp.ToUniversalTime());
Directory.Delete(root, true);
}
}
Output
Unspecified
True
How It Works
No kind was given. A DateTime built without a kind has KindUnspecified, which the first line prints.
SetCreationTime reads it as local. That is why this method is the right one for a value that comes from the user's clock.
Check in UTC.stamp.ToUniversalTime() treats the Unspecified value as local too, so it equals what the file stores in UTC, and the check prints True.
Example 3 — Back-Date Two Files and Find the Old One
Set two different creation dates and list the files created before a cutoff.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-setcreationtime-old");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string oldFile = Path.Combine(root, "old.txt");
string newFile = Path.Combine(root, "new.txt");
File.WriteAllText(oldFile, "1");
File.WriteAllText(newFile, "2");
File.SetCreationTime(oldFile, new DateTime(2025, 6, 1));
File.SetCreationTime(newFile, new DateTime(2026, 3, 1));
DateTime cutoff = new DateTime(2026, 1, 1);
foreach (string file in new[] { oldFile, newFile }) {
if (File.GetCreationTime(file) < cutoff) {
Console.WriteLine(Path.GetFileName(file));
}
}
Directory.Delete(root, true);
}
}
Output
old.txt
How It Works
Fake history. The two files are created a moment apart, but SetCreationTime gives them dates that are nine months apart.
A cutoff. The cutoff is 1 January 2026. old.txt is dated June 2025, which is before it, and new.txt is dated March 2026, which is after it.
Only the old file is listed. The loop prints names for files below the cutoff, so just old.txt appears. This is how you test a cleanup rule without waiting months.
Frequently Asked Questions
It changes the creation time of a file to the DateTime you pass, without changing the content.
Use this form for a value in local time. When the value must be exact on every machine, use SetCreationTimeUtc with a DateTime whose Kind is Utc.
The call throws an IOException (FileNotFoundException). Create the file first, or check File.Exists.
Many Linux file systems do not store a creation time, or .NET cannot read or change it there. The value may be approximated from other times. For portable code use the last write time, and expect creation-time code to work best on Windows and macOS.
Call File.GetCreationTime(path). Pass a fixed DateTime when you set it, so the value you read back is predictable.