C# File SetLastAccessTimeUtc() Method

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

Definition

In C#, File.SetLastAccessTimeUtc() changes the last access 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 SetLastAccessTime.

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

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

Console.WriteLine(File.GetLastAccessTimeUtc(path).Year);   // 2026
CallWhat it does
File.SetLastAccessTimeUtc(path, time)Sets the last access time to time.
File.SetLastAccessTimeUtc(path, DateTime.UtcNow)Sets it to the current moment, a "touch".
File.SetLastAccessTimeUtc(path, new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc))Gives the file a fixed old date, handy for testing.

FileInfo.LastAccessTimeUtc 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 SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc);

Parameters

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

Return Value

Return ValueDescription
voidNothing is returned. Read the result back with GetLastAccessTimeUtc.

C# Examples

Example 1 — Set a UTC Access Time and Print It Exactly

Use the round-trip format to see the whole value that was stored.

C#
using System;
using System.IO;

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

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

        Directory.Delete(root, true);
    }
}

How It Works

  1. UTC in, UTC out. The value is marked DateTimeKind.Utc, so the file holds 14:45 UTC on 5 March 2026.
  2. Read with the UTC getter. GetLastAccessTimeUtc gives the moment back without any time-zone conversion.
  3. Same text everywhere. The "o" format ends with Z, which makes the printed line independent of the computer.

Example 2 — Shift an Access Time by 36 Hours

Read the stored value, add hours with AddHours, and set it again.

C#
using System;
using System.IO;

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

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

        File.SetLastAccessTimeUtc(path, new DateTime(2026, 1, 15, 9, 30, 0, DateTimeKind.Utc));
        DateTime first = File.GetLastAccessTimeUtc(path);
        Console.WriteLine(first.ToString("yyyy-MM-dd HH:mm"));

        File.SetLastAccessTimeUtc(path, first.AddHours(36));
        Console.WriteLine(File.GetLastAccessTimeUtc(path).ToString("yyyy-MM-dd HH:mm"));

        Directory.Delete(root, true);
    }
}

How It Works

  1. Start from a known value. The first line prints 09:30 on 15 January, which was just set.
  2. Read, change, set. The getter returns a DateTime with kind Utc. AddHours(36) creates a new value and keeps the kind.
  3. Whole days and hours. 36 hours is one day and twelve hours, so 09:30 becomes 21:30 on the next day. UTC has no daylight-saving jumps to spoil this sum.

Example 3 — A Helper That Tells If a File Is Stale

Set an old UTC time and a current one, and test each with the same helper.

C#
using System;
using System.IO;

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

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

        File.SetLastAccessTimeUtc(path, new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc));
        Console.WriteLine(IsStale(path, 30));

        File.SetLastAccessTimeUtc(path, DateTime.UtcNow);
        Console.WriteLine(IsStale(path, 30));

        static bool IsStale(string file, int days) {
            return (DateTime.UtcNow - File.GetLastAccessTimeUtc(file)).TotalDays > days;
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. A helper with a rule. IsStale subtracts the stored UTC time from the current UTC time and checks whether the gap is bigger than the number of days.
  2. An old date. A time in 2020 is far more than 30 days ago, so the first call prints True.
  3. Touch it to now. SetLastAccessTimeUtc with DateTime.UtcNow makes the gap close to zero, so the second call prints False.

Frequently Asked Questions

It changes the last access 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 SetLastAccessTime 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.
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.GetLastAccessTimeUtc(path). Pass a fixed DateTime when you set it, so the value you read back is predictable.

Next: SetLastWriteTime()

Change the last write time of a file using a local DateTime.

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

11 people found this page helpful