C# File GetLastWriteTimeUtc() Method

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

Definition

In C#, File.GetLastWriteTimeUtc() returns the last write time of a file: the moment it was last changed. The answer is a DateTime in UTC (Kind is Utc).

UTC does not change with the time zone or daylight saving, so it is the best form for logs, comparisons, and data you send to other machines. For a missing file the method does not throw; it returns 1601-01-01 00:00 UTC, so check File.Exists first. The local form is GetLastWriteTime.

This is a static method of System.IO.File. Call it as File.GetLastWriteTimeUtc(...); there is no new File(). For an object that remembers one file, see FileInfo.

Syntax

Pass the path of an existing file and store the DateTime that comes back.

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

DateTime when = File.GetLastWriteTimeUtc(path);

Console.WriteLine(when.Kind);   // Utc
CallWhat it does
File.GetLastWriteTimeUtc(path)Returns the last write time as UTC.
File.GetLastWriteTimeUtc(path).ToString("yyyy-MM-dd HH:mm")Formats the value as text with a fixed pattern.
File.GetLastWriteTimeUtc(missingPath)No exception. Returns 1601-01-01 UTC.

FileInfo.LastWriteTimeUtc gives the same value as a property. A FileInfo may keep an older value until you call Refresh(), while File.GetLastWriteTimeUtc asks the file system every time.

Library declarations
C#
public static DateTime GetLastWriteTimeUtc(string path);

Parameters

ParameterDescription
pathThe file to read the time from. It may be absolute or relative.

Return Value

Return ValueDescription
DateTimeThe last write time, with Kind set to Utc. For a missing file the value is January 1, 1601 UTC.

C# Examples

Example 1 — Read a UTC Write Time with the Round-Trip Format

Set a fixed UTC write time and print the exact value.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-getlastwritetimeutc");
        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, 4, 20, 16, 5, 0, DateTimeKind.Utc));

        Console.WriteLine(File.GetLastWriteTimeUtc(path).ToString("o"));

        Directory.Delete(root, true);
    }
}

How It Works

  1. Set the value you want to see. A real write time changes with the clock, so a fixed UTC value makes the output predictable.
  2. The UTC getter. GetLastWriteTimeUtc returns the moment without any time-zone conversion.
  3. Seven digits and a Z. The "o" format prints the fraction of a second and ends with Z, a clear sign that the value is UTC.

Example 2 — The Default Value of a Missing File

Print the default date and compare it with the Windows file time zero.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-getlastwritetimeutc-missing");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string missing = Path.Combine(root, "absent.txt");

        DateTime changed = File.GetLastWriteTimeUtc(missing);

        Console.WriteLine(changed.ToString("yyyy-MM-dd HH:mm"));
        Console.WriteLine(changed == DateTime.FromFileTimeUtc(0));

        Directory.Delete(root, true);
    }
}

How It Works

  1. No such file. absent.txt was never created. The method does not throw.
  2. The same default as the other getters. The value is midnight on 1 January 1601 UTC.
  3. Detect it in code. Comparing with DateTime.FromFileTimeUtc(0) is one way to notice "no file", although File.Exists is clearer.

Example 3 — Decide Whether a Backup Copy Is Out of Date

Compare UTC write times of a source and a destination, the core of a simple sync tool.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-getlastwritetimeutc-sync");
        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, 2, 0, 0, 0, DateTimeKind.Utc));
        Console.WriteLine(NeedsCopy(source, target));

        File.Copy(source, target);
        File.SetLastWriteTimeUtc(target, new DateTime(2026, 1, 2, 0, 0, 0, DateTimeKind.Utc));
        Console.WriteLine(NeedsCopy(source, target));

        File.SetLastWriteTimeUtc(source, new DateTime(2026, 1, 3, 0, 0, 0, DateTimeKind.Utc));
        Console.WriteLine(NeedsCopy(source, target));

        static bool NeedsCopy(string src, string dst) {
            if (!File.Exists(dst)) {
                return true;
            }
            return File.GetLastWriteTimeUtc(src) > File.GetLastWriteTimeUtc(dst);
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. A tiny rule. NeedsCopy says yes when the target is missing, or when the source was written later than the target.
  2. First call: no target. The target does not exist yet, so the answer is True.
  3. Second call: same time. After the copy, both files carry the same UTC stamp, which is not "later", so the answer is False. The stamp is set by hand so the result does not depend on how the system copies timestamps.
  4. Third call: newer source. Moving the source stamp one day forward makes it later than the target, so the answer is True again. UTC avoids daylight-saving mistakes.

Frequently Asked Questions

A DateTime with the last write time of the file, as UTC (Kind is Utc).
Use it for logs, comparisons, and data you store or send. UTC has no time zone or daylight-saving shifts, so two machines agree on the value.
It does not throw. It returns January 1, 1601 UTC. Check File.Exists first when a missing file is possible.
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.SetLastWriteTimeUtc(path, dateTime). Pass a fixed DateTime when you want a predictable value for tests.

Next: Move()

Move or rename a file. The original path is gone afterwards.

Move() 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.

14 people found this page helpful