In C#, File.GetLastWriteTime() returns the last write time of a file: the moment it was last changed. 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 GetLastWriteTimeUtc, which is not affected by time zones.
This is a static method of System.IO.File. Call it as File.GetLastWriteTime(...); 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-getlastwritetime.txt");
File.WriteAllText(path, "demo");
DateTime when = File.GetLastWriteTime(path);
Console.WriteLine(when.Kind); // Local
No exception. Returns 1601-01-01 UTC in local time.
FileInfo.LastWriteTime gives the same value as a property. A FileInfo may keep an older value until you call Refresh(), while File.GetLastWriteTime asks the file system every time.
Library declarations
C#
public static DateTime GetLastWriteTime(string path);
Parameters
Parameter
Description
path
The file to read the time from. It may be absolute or relative.
Return Value
Return Value
Description
DateTime
The last write 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 Write Time You Set Yourself
Set a fixed local write time and read it back with GetLastWriteTime.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-getlastwritetime");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "stamp.txt");
File.WriteAllText(path, "demo");
File.SetLastWriteTime(path, new DateTime(2026, 1, 15, 9, 30, 0));
DateTime changed = File.GetLastWriteTime(path);
Console.WriteLine(changed.ToString("yyyy-MM-dd HH:mm"));
Console.WriteLine(changed.Kind);
Directory.Delete(root, true);
}
}
Output
2026-01-15 09:30
Local
How It Works
The real value moves.WriteAllText stamps the file with the current time, which would make the output different on every run.
Override it.SetLastWriteTime replaces that stamp with a fixed one, and GetLastWriteTime reads it back as local time.
A plain, stable output. The formatted text and the Local kind are the same on every computer, because the value was set as local.
Example 2 — Is the File Recently Changed?
Compare the write time with five minutes ago, before and after back-dating it.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-getlastwritetime-fresh");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "log.txt");
File.WriteAllText(path, "entry");
DateTime limit = DateTime.Now.AddMinutes(-5);
bool fresh = File.GetLastWriteTime(path) > limit;
Console.WriteLine(fresh);
File.SetLastWriteTime(path, new DateTime(2020, 1, 1));
fresh = File.GetLastWriteTime(path) > limit;
Console.WriteLine(fresh);
Directory.Delete(root, true);
}
}
Output
True
False
How It Works
A moving limit.DateTime.Now.AddMinutes(-5) is the moment five minutes ago. A file written just now is newer than that.
Newly written is fresh. The first check prints True, because WriteAllText only just changed the file.
Back-date and test again. After the stamp is moved to 2020, the same comparison prints False. This pattern is used for cache files and log rotation.
Example 3 — Writing to a File Updates Its Write Time
Back-date a file, append text, and see the write time jump forward.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-getlastwritetime-append");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "data.txt");
File.WriteAllText(path, "demo");
File.SetLastWriteTime(path, new DateTime(2020, 1, 1));
Console.WriteLine(File.GetLastWriteTime(path).Year);
File.AppendAllText(path, "more");
Console.WriteLine(File.GetLastWriteTime(path).Year > 2020);
Console.WriteLine(File.ReadAllText(path));
Directory.Delete(root, true);
}
}
Output
2020
True
demomore
How It Works
Old stamp on purpose. After SetLastWriteTime the year reads 2020.
Any write refreshes it.AppendAllText changes the content, so the system stamps the file with the current time. The year is now later than 2020, and the check prints True.
The text is the proof. The file contains demomore, so there was a real write. The program prints a year comparison instead of the current time, which keeps the output stable.
Frequently Asked Questions
A DateTime with the last write time of the file, as local time (Kind is Local).
Use the Utc form for comparisons, storage, and anything shared between computers. Use GetLastWriteTime 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.
The last write time. It changes when the content is written, and it is the one backup, sync, and build tools compare. Use the Utc form to avoid time-zone and daylight-saving surprises.
Call File.SetLastWriteTime(path, dateTime). Pass a fixed DateTime when you want a predictable value for tests.