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.
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
Parameter
Description
path
The file to change. It must exist.
lastAccessTimeUtc
The new last access time. Treated as UTC; use DateTimeKind.Utc.
Return Value
Return Value
Description
void
Nothing 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);
}
}
Output
2026-03-05T14:45:00.0000000Z
How It Works
UTC in, UTC out. The value is marked DateTimeKind.Utc, so the file holds 14:45 UTC on 5 March 2026.
Read with the UTC getter.GetLastAccessTimeUtc gives the moment back without any time-zone conversion.
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);
}
}
Output
2026-01-15 09:30
2026-01-16 21:30
How It Works
Start from a known value. The first line prints 09:30 on 15 January, which was just set.
Read, change, set. The getter returns a DateTime with kind Utc. AddHours(36) creates a new value and keeps the kind.
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);
}
}
Output
True
False
How It Works
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.
An old date. A time in 2020 is far more than 30 days ago, so the first call prints True.
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.