C# Program to get the last write time of a file

To get the last write time of a file in C#, you can use the LastWriteTime property of the FileInfo class. This property returns a DateTime object representing when the file was last modified.

You can also use the static File.GetLastWriteTime() method as an alternative approach for retrieving the last write time without creating a FileInfo instance.

Syntax

Using FileInfo class −

FileInfo fileInfo = new FileInfo("filename.txt");
DateTime lastWrite = fileInfo.LastWriteTime;

Using File.GetLastWriteTime() method −

DateTime lastWrite = File.GetLastWriteTime("filename.txt");

Using FileInfo Class

The FileInfo class provides comprehensive file information including creation time, last access time, and last write time −

using System;
using System.IO;

public class Program {
    public static void Main() {
        using (StreamWriter sw = new StreamWriter("sample.txt")) {
            sw.WriteLine("This is a sample file.");
            sw.WriteLine("Last write time will be recorded.");
        }
        
        FileInfo file = new FileInfo("sample.txt");
        
        // file creation time
        DateTime creationTime = file.CreationTime;
        Console.WriteLine("Creation Time: " + creationTime);
        
        // last access time
        DateTime lastAccessTime = file.LastAccessTime;
        Console.WriteLine("Last Access Time: " + lastAccessTime);
        
        // last write time
        DateTime lastWriteTime = file.LastWriteTime;
        Console.WriteLine("Last Write Time: " + lastWriteTime);
    }
}

The output of the above code is −

Creation Time: 12/15/2023 3:45:22 PM
Last Access Time: 12/15/2023 3:45:22 PM
Last Write Time: 12/15/2023 3:45:22 PM

Using File.GetLastWriteTime() Method

For a simpler approach when you only need the last write time, use the static File.GetLastWriteTime() method −

using System;
using System.IO;

public class Program {
    public static void Main() {
        // Create a sample file
        File.WriteAllText("example.txt", "Sample content for demonstration.");
        
        // Get last write time using File.GetLastWriteTime()
        DateTime lastWrite = File.GetLastWriteTime("example.txt");
        Console.WriteLine("Last Write Time: " + lastWrite);
        
        // Modify the file
        System.Threading.Thread.Sleep(2000); // Wait 2 seconds
        File.AppendAllText("example.txt", "\nAdditional content added.");
        
        // Check the updated last write time
        DateTime updatedLastWrite = File.GetLastWriteTime("example.txt");
        Console.WriteLine("Updated Last Write Time: " + updatedLastWrite);
    }
}

The output of the above code is −

Last Write Time: 12/15/2023 3:45:25 PM
Updated Last Write Time: 12/15/2023 3:45:27 PM

Comparison

Method Use Case Performance
FileInfo.LastWriteTime When you need multiple file properties Better for multiple operations on the same file
File.GetLastWriteTime() When you only need the last write time Better for single operations

Conclusion

Getting the last write time of a file in C# is straightforward using either FileInfo.LastWriteTime property or File.GetLastWriteTime() method. Choose FileInfo when you need multiple file properties, or use the static File method for simple, one-time queries.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements