C# File SetLastWriteTimeUtc() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

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.

C#
string path = Path.Combine(Path.GetTempPath(), "codetofun-setlastwritetimeutc.txt");
File.WriteAllText(path, "demo");

File.SetLastWriteTimeUtc(path, new DateTime(2026, 1, 15, 9, 30, 0, DateTimeKind.Utc));

Console.WriteLine(File.GetLastWriteTimeUtc(path).Year);   // 2026
CallWhat it does
File.SetLastWriteTimeUtc(path, time)Sets the last write time to time.
File.SetLastWriteTimeUtc(path, DateTime.UtcNow)Sets it to the current moment, a "touch".
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

ParameterDescription
pathThe file to change. It must exist.
lastWriteTimeUtcThe new last write time. Treated as UTC; use DateTimeKind.Utc.

Return Value

Return ValueDescription
voidNothing 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);
    }
}

How It Works

  1. 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.
  2. No conversion. The UTC setter and the UTC getter use the same zone, so the value comes back exactly as it went in.
  3. 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);
    }
}

How It Works

  1. Remember the stamp. The variable stamp holds the value that should survive the edit.
  2. 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.
  3. 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);
    }
}

How It Works

  1. A source with a known stamp. source.txt is dated 15 January 2026, 09:30 UTC.
  2. 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.
  3. 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.

Next: WriteAllBytes()

Save a byte array to a file, creating it or replacing it.

WriteAllBytes() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful