In C#, File.SetLastWriteTime() changes the last write time that the file system stores for a file. You pass a DateTime that is read as local time, the same zone that GetLastWriteTime 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 SetLastWriteTimeUtc.
This is a static method of System.IO.File. Call it as File.SetLastWriteTime(...); 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.SetLastWriteTime(path, new DateTime(2020, 1, 1, 0, 0, 0))
Gives the file a fixed old date, handy for testing.
FileInfo.LastWriteTime 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 SetLastWriteTime(string path, DateTime lastWriteTime);
Parameters
Parameter
Description
path
The file to change. It must exist.
lastWriteTime
The new last write time. Treated as local time unless its Kind says otherwise.
Return Value
Return Value
Description
void
Nothing is returned. Read the result back with GetLastWriteTime.
C# Examples
Example 1 — Set a Last Write Time Without Changing the Content
Give a file a fixed local write time and check that the text is untouched.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-setlastwritetime");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "stamp.txt");
File.WriteAllText(path, "demo");
File.SetLastWriteTime(path, new DateTime(2026, 1, 15, 9, 30, 0));
Console.WriteLine(File.GetLastWriteTime(path).ToString("yyyy-MM-dd HH:mm"));
Console.WriteLine(File.ReadAllText(path));
Directory.Delete(root, true);
}
}
Output
2026-01-15 09:30
demo
How It Works
A new write time.SetLastWriteTime writes the stamp that was passed. It is read as local time.
The date reads back. The first output line is the exact stamp, with fixed formatting.
The content is not a "write". Setting the stamp does not rewrite the file, so the text is still demo.
Example 2 — Touch a File to Mark It as Changed Now
Back-date a file, then set its write time to the current moment, like the Unix touch command.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-setlastwritetime-touch");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "touch.txt");
File.WriteAllText(path, "demo");
File.SetLastWriteTime(path, new DateTime(2020, 1, 1));
Console.WriteLine(File.GetLastWriteTime(path).Year);
File.SetLastWriteTime(path, DateTime.Now);
Console.WriteLine(File.GetLastWriteTime(path).Year > 2020);
Directory.Delete(root, true);
}
}
Output
2020
True
How It Works
Start in the past. The first call moves the stamp to 2020, and the year check proves it.
Touch.SetLastWriteTime(path, DateTime.Now) sets the stamp to the present without changing a single byte of the content.
Print a year, not the time. The program prints only a comparison with the year 2020, because the exact current time would be different on every run.
Example 3 — List Files from Oldest to Newest
Give three files different write times and sort them with LINQ.
C#
using System;
using System.IO;
using System.Linq;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-setlastwritetime-order");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string[] names = { "a.txt", "b.txt", "c.txt" };
foreach (string name in names) {
File.WriteAllText(Path.Combine(root, name), "x");
}
File.SetLastWriteTime(Path.Combine(root, "a.txt"), new DateTime(2026, 1, 1));
File.SetLastWriteTime(Path.Combine(root, "b.txt"), new DateTime(2025, 6, 1));
File.SetLastWriteTime(Path.Combine(root, "c.txt"), new DateTime(2026, 3, 1));
foreach (string file in Directory.GetFiles(root).OrderBy(f => File.GetLastWriteTime(f))) {
Console.WriteLine(Path.GetFileName(file));
}
Directory.Delete(root, true);
}
}
Output
b.txt
a.txt
c.txt
How It Works
Three different dates.b.txt is the oldest (June 2025), a.txt is in the middle (January 2026), and c.txt is the newest (March 2026).
Sort by the stamp.OrderBy(f => File.GetLastWriteTime(f)) asks the file system for each write time and sorts from old to new. It needs using System.Linq;.
Only names are printed. The listing from GetFiles has no promised order, so the sort makes the result predictable. Use OrderByDescending for newest first.
Frequently Asked Questions
It changes the last write 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 SetLastWriteTimeUtc with a DateTime whose Kind is Utc.
The call throws an IOException (FileNotFoundException). Create the file first, or check File.Exists.
The last write time. It changes when the content is written, and it is the one backup, sync, and build tools compare. Use the Utc form to avoid time-zone and daylight-saving surprises.
Call File.GetLastWriteTime(path). Pass a fixed DateTime when you set it, so the value you read back is predictable.