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.
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
Parameter
Description
path
The file to change. It must exist.
lastAccessTime
The new last access time. Treated as local time unless its Kind says otherwise.
Return Value
Return Value
Description
void
Nothing 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);
}
}
Output
True
2026-02-10 08:00
How It Works
A fixed local time. The value is 8:00 on 10 February 2026, with no kind given, which SetLastAccessTime reads as local time.
Set, then read at once. The program does not open the file in between, so nothing else can move the access time.
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);
}
}
Output
2026-01-01 08:00
2026-03-01 08:00
How It Works
Write time first.SetLastWriteTime gives the file a January date.
Then the access time.SetLastAccessTime sets a March date. It only touches the access time and leaves the January write time alone.
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);
}
}
Output
3
How It Works
One stamp for all. The same stamp variable is used for every file in the loop.
Create, set, check. Each pass creates a file, sets its access time, and compares the value read back with the stamp.
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.