C# File GetLastAccessTimeUtc() Method

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

Definition

In C#, File.GetLastAccessTimeUtc() returns the last access time of a file: the moment it was last opened or read. 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 GetLastAccessTime.

This is a static method of System.IO.File. Call it as File.GetLastAccessTimeUtc(...); 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-getlastaccesstimeutc.txt");
File.WriteAllText(path, "demo");

DateTime when = File.GetLastAccessTimeUtc(path);

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

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

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

Parameters

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

Return Value

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

C# Examples

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

Set a fixed UTC access time and print the whole value.

C#
using System;
using System.IO;

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

        string path = Path.Combine(root, "stamp.txt");
        File.WriteAllText(path, "demo");

        File.SetLastAccessTimeUtc(path, new DateTime(2026, 2, 10, 8, 0, 0, DateTimeKind.Utc));

        DateTime seen = File.GetLastAccessTimeUtc(path);
        Console.WriteLine(seen.ToString("o"));

        Directory.Delete(root, true);
    }
}

How It Works

  1. A fixed UTC stamp. The setter receives a value marked DateTimeKind.Utc, so the file holds 08:00 UTC on 10 February 2026.
  2. The getter returns UTC. GetLastAccessTimeUtc hands back the same moment with Kind Utc.
  3. Exact text. The "o" format ends with Z for UTC and shows seven digits of fraction, so the printed line is identical on every computer.

Example 2 — Missing File: Year and Kind

Check the year and the kind of the value returned for a file that does not exist.

C#
using System;
using System.IO;

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

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

        DateTime seen = File.GetLastAccessTimeUtc(missing);

        Console.WriteLine(seen.Year);
        Console.WriteLine(seen.Kind);

        Directory.Delete(root, true);
    }
}

How It Works

  1. A file that is not there. No none.txt was created, and the program does not check for it on purpose.
  2. The getter does not throw. It returns the default date, so the year printed is 1601.
  3. The kind is still Utc. Even the default value is marked as UTC, so converting it with ToLocalTime() would work as usual.

Example 3 — Compare the Access Time with the Write Time

Give the two timestamps different dates and measure the gap.

C#
using System;
using System.IO;

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

        string path = Path.Combine(root, "gap.txt");
        File.WriteAllText(path, "demo");

        File.SetLastWriteTimeUtc(path, new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc));
        File.SetLastAccessTimeUtc(path, new DateTime(2026, 3, 1, 0, 0, 0, DateTimeKind.Utc));

        DateTime written = File.GetLastWriteTimeUtc(path);
        DateTime opened = File.GetLastAccessTimeUtc(path);

        Console.WriteLine(opened > written);
        Console.WriteLine((opened - written).TotalDays);

        Directory.Delete(root, true);
    }
}

How It Works

  1. Two separate stamps. A file keeps its write time and its access time apart. Setting one does not change the other.
  2. Opened after written. 1 March is after 1 January, so opened > written prints True. Normally a file is read after it was written, as here.
  3. The gap. January has 31 days and February has 28 in 2026, so the difference is 59 days. UTC is used so that daylight saving cannot add or remove an hour.

Frequently Asked Questions

A DateTime with the last access 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.
Operating systems often delay or disable last-access updates to save disk writes (Windows can turn them off, Linux uses relatime or noatime). The value can therefore be old even if the file was just read.
Call File.SetLastAccessTimeUtc(path, dateTime). Pass a fixed DateTime when you want a predictable value for tests.

Next: GetLastWriteTime()

Read when a file was last changed, as a local DateTime.

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

12 people found this page helpful