C# File SetLastAccessTime() Method

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

Definition

In C#, File.SetLastAccessTime() changes the last access time that the file system stores for a file. You pass a DateTime that is read as local time, the same zone that GetLastAccessTime shows.

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 UTC form is SetLastAccessTimeUtc.

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

File.SetLastAccessTime(path, new DateTime(2026, 1, 15, 9, 30, 0));

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

FileInfo.LastAccessTime 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 SetLastAccessTime(string path, DateTime lastAccessTime);

Parameters

ParameterDescription
pathThe file to change. It must exist.
lastAccessTimeThe new last access time. Treated as local time unless its Kind says otherwise.

Return Value

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

C# Examples

Example 1 — Set a Last Access Time and Read It Back

Give a file a fixed local access time and confirm it.

C#
using System;
using System.IO;

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

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

        DateTime stamp = new DateTime(2026, 2, 10, 8, 0, 0);
        File.SetLastAccessTime(path, stamp);

        Console.WriteLine(File.GetLastAccessTime(path) == stamp);
        Console.WriteLine(File.GetLastAccessTime(path).ToString("yyyy-MM-dd HH:mm"));

        Directory.Delete(root, true);
    }
}

How It Works

  1. A fixed local time. The value is 8:00 on 10 February 2026, with no kind given, which SetLastAccessTime reads as local time.
  2. Set, then read at once. The program does not open the file in between, so nothing else can move the access time.
  3. Equal and formatted. The first line prints True, and the second shows the date and minute that were set.

Example 2 — Setting Access Time Leaves Write Time Alone

Set both stamps and see that each keeps its own value.

C#
using System;
using System.IO;

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

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

        File.SetLastWriteTime(path, new DateTime(2026, 1, 1, 8, 0, 0));
        File.SetLastAccessTime(path, new DateTime(2026, 3, 1, 8, 0, 0));

        Console.WriteLine(File.GetLastWriteTime(path).ToString("yyyy-MM-dd HH:mm"));
        Console.WriteLine(File.GetLastAccessTime(path).ToString("yyyy-MM-dd HH:mm"));

        Directory.Delete(root, true);
    }
}

How It Works

  1. Write time first. SetLastWriteTime gives the file a January date.
  2. Then the access time. SetLastAccessTime sets a March date. It only touches the access time and leaves the January write time alone.
  3. Both are kept. The two printed lines show two different dates for the same file, which proves that the stamps are independent.

Example 3 — Give a Whole Batch the Same Access Time

Loop over three files, set one stamp on all of them, and count the matches.

C#
using System;
using System.IO;

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

        DateTime stamp = new DateTime(2026, 2, 1, 12, 0, 0);
        int matches = 0;

        foreach (string name in new[] { "a.txt", "b.txt", "c.txt" }) {
            string path = Path.Combine(root, name);
            File.WriteAllText(path, "x");
            File.SetLastAccessTime(path, stamp);

            if (File.GetLastAccessTime(path) == stamp) {
                matches++;
            }
        }

        Console.WriteLine(matches);

        Directory.Delete(root, true);
    }
}

How It Works

  1. One stamp for all. The same stamp variable is used for every file in the loop.
  2. Create, set, check. Each pass creates a file, sets its access time, and compares the value read back with the stamp.
  3. A running count. All three comparisons are true, so matches ends at 3. A count like this is a quick way to verify a bulk change.

Frequently Asked Questions

It changes the last access time of a file to the DateTime you pass, without changing the content.
Use this form for a value in local time. When the value must be exact on every machine, use SetLastAccessTimeUtc with a DateTime whose Kind is Utc.
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.GetLastAccessTime(path). Pass a fixed DateTime when you set it, so the value you read back is predictable.

Next: SetLastAccessTimeUtc()

Change the last access time of a file using a UTC DateTime.

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

10 people found this page helpful