In C#, File.SetLastWriteTimeUtc() changes the last write time that the file system stores for a file. You pass a DateTime that is read as UTC, so give it DateTimeKind.Utc.
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 local form is SetLastWriteTime.
This is a static method of System.IO.File. Call it as File.SetLastWriteTimeUtc(...); 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.SetLastWriteTimeUtc(path, new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc))
Gives the file a fixed old date, handy for testing.
FileInfo.LastWriteTimeUtc 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 SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc);
Parameters
Parameter
Description
path
The file to change. It must exist.
lastWriteTimeUtc
The new last write time. Treated as UTC; use DateTimeKind.Utc.
Return Value
Return Value
Description
void
Nothing is returned. Read the result back with GetLastWriteTimeUtc.
C# Examples
Example 1 — Set a UTC Write Time and Print It Exactly
Store a fixed UTC value and read it back with the round-trip format.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-setlastwritetimeutc");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "stamp.txt");
File.WriteAllText(path, "demo");
File.SetLastWriteTimeUtc(path, new DateTime(2026, 6, 30, 23, 58, 0, DateTimeKind.Utc));
Console.WriteLine(File.GetLastWriteTimeUtc(path).ToString("o"));
Directory.Delete(root, true);
}
}
Output
2026-06-30T23:58:00.0000000Z
How It Works
A late evening in UTC. 23:58 on 30 June is close to midnight, a good test for time-zone code, because most time zones are already on the next day.
No conversion. The UTC setter and the UTC getter use the same zone, so the value comes back exactly as it went in.
Identical everywhere. The "o" format and its final Z print the same text on every computer in every time zone.
Example 2 — Edit a File, Then Put the Old Write Time Back
Show that a write changes the stamp, and that SetLastWriteTimeUtc can restore it.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-setlastwritetimeutc-restore");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "doc.txt");
File.WriteAllText(path, "draft");
DateTime stamp = new DateTime(2026, 1, 15, 9, 30, 0, DateTimeKind.Utc);
File.SetLastWriteTimeUtc(path, stamp);
File.WriteAllText(path, "changed");
Console.WriteLine(File.GetLastWriteTimeUtc(path) == stamp);
File.SetLastWriteTimeUtc(path, stamp);
Console.WriteLine(File.GetLastWriteTimeUtc(path) == stamp);
Directory.Delete(root, true);
}
}
Output
False
True
How It Works
Remember the stamp. The variable stamp holds the value that should survive the edit.
The edit moves it.WriteAllText rewrites the file, and the system stamps it with the current time, so the comparison with the old value is False.
Restore it. Calling SetLastWriteTimeUtc(path, stamp) again puts the old value back, and the second comparison prints True. Tools that must not disturb file dates use this trick.
Example 3 — Make a Copy Carry the Source Write Time
Copy a file and give the copy exactly the same UTC write time as the original.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-setlastwritetimeutc-copy");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string source = Path.Combine(root, "source.txt");
string target = Path.Combine(root, "target.txt");
File.WriteAllText(source, "data");
File.SetLastWriteTimeUtc(source, new DateTime(2026, 1, 15, 9, 30, 0, DateTimeKind.Utc));
File.Copy(source, target);
File.SetLastWriteTimeUtc(target, File.GetLastWriteTimeUtc(source));
Console.WriteLine(File.GetLastWriteTimeUtc(target) == File.GetLastWriteTimeUtc(source));
Console.WriteLine(File.ReadAllText(target));
Directory.Delete(root, true);
}
}
Output
True
data
How It Works
A source with a known stamp.source.txt is dated 15 January 2026, 09:30 UTC.
Copy, then align. A system may or may not copy the timestamp. Reading the source value and setting it on the target makes sure that the two files carry the same one.
Equal and complete. The comparison prints True, and the copied content data proves that the file itself was copied too.
Frequently Asked Questions
It changes the last write time of a file to the DateTime you pass, without changing the content.
Use this Utc form when your value is already UTC or must be exact on every machine. The local form SetLastWriteTime is for values that come from the user's own clock.
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.GetLastWriteTimeUtc(path). Pass a fixed DateTime when you set it, so the value you read back is predictable.