C# File GetLastAccessTime() Method

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

Definition

In C#, File.GetLastAccessTime() returns the last access time of a file: the moment it was last opened or read. The answer is a DateTime in the computer's local time zone (Kind is Local).

The method does not throw for a missing file. It returns 1601-01-01 00:00 UTC converted to local time, so check File.Exists first. For comparisons between computers, prefer GetLastAccessTimeUtc, which is not affected by time zones.

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

DateTime when = File.GetLastAccessTime(path);

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

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

Library declarations
C#
public static DateTime GetLastAccessTime(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 Local. For a missing file the value is January 1, 1601 UTC, converted to local time.

C# Examples

Example 1 — Read a Last Access Time You Set Yourself

Set a fixed access time, then read it right back before anything else touches the file.

C#
using System;
using System.IO;

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

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

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

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

        Directory.Delete(root, true);
    }
}

How It Works

  1. Do not read before you check. The real access time can move whenever a file is read, so the example sets a value first and does not read the file again.
  2. The getter returns it. GetLastAccessTime returns the 8:00 stamp on 10 February 2026 as a local DateTime.
  3. Local by definition. The last line prints Local. For a value that must match on every machine, use GetLastAccessTimeUtc.

Example 2 — A Missing File Gives the 1601 Date

See that no exception is thrown when the file does not exist.

C#
using System;
using System.IO;

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

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

        DateTime seen = File.GetLastAccessTime(missing);

        Console.WriteLine(File.Exists(missing));
        Console.WriteLine(seen.ToUniversalTime().ToString("yyyy-MM-dd"));

        Directory.Delete(root, true);
    }
}

How It Works

  1. A path that leads nowhere. gone.txt was never created, so Exists prints False.
  2. No exception. The getter quietly returns the default date for a missing file. A program that skips the Exists check could treat a missing file as very old.
  3. Normalise before printing. ToUniversalTime() converts the local result back to UTC, so every time zone prints 1601-01-01.

Example 3 — Find Files Not Opened Recently

Give two files different access dates and list the one that is older than a cutoff.

C#
using System;
using System.IO;

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

        string stale = Path.Combine(root, "stale.txt");
        string fresh = Path.Combine(root, "fresh.txt");
        File.WriteAllText(stale, "1");
        File.WriteAllText(fresh, "2");

        File.SetLastAccessTime(stale, new DateTime(2025, 3, 1, 8, 0, 0));
        File.SetLastAccessTime(fresh, new DateTime(2026, 2, 10, 8, 0, 0));

        DateTime cutoff = new DateTime(2025, 12, 31);
        foreach (string file in new[] { stale, fresh }) {
            if (File.GetLastAccessTime(file) < cutoff) {
                Console.WriteLine(Path.GetFileName(file));
            }
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. Two files, two dates. stale.txt is stamped March 2025 and fresh.txt is stamped February 2026.
  2. Compare with a cutoff. DateTime values support < directly, so GetLastAccessTime(file) < cutoff reads like plain English.
  3. Only the stale file matches. The loop prints stale.txt. In real programs remember the warning: access times may not be updated, so use this for hints, not for deleting files.

Frequently Asked Questions

A DateTime with the last access time of the file, as local time (Kind is Local).
Use the Utc form for comparisons, storage, and anything shared between computers. Use GetLastAccessTime when you only show the time to the user in their own time zone.
It does not throw. It returns January 1, 1601 UTC, converted to local time. 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.SetLastAccessTime(path, dateTime). Pass a fixed DateTime when you want a predictable value for tests.

Next: GetLastAccessTimeUtc()

Read when a file was last opened or read, as UTC time that does not depend on the time zone.

GetLastAccessTimeUtc() 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