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

To get the last access time of a file in C#, you can use the LastAccessTime property of the FileInfo class or the static File.GetLastAccessTime() method. The last access time represents when the file was last opened or read.

Syntax

Using the FileInfo class −

FileInfo fileInfo = new FileInfo("filepath");
DateTime lastAccess = fileInfo.LastAccessTime;

Using the static File.GetLastAccessTime() method −

DateTime lastAccess = File.GetLastAccessTime("filepath");

Using FileInfo Class

The FileInfo class provides an object-oriented approach to working with files. Here's how to get the last access time −

using System;
using System.IO;

public class Program {
   public static void Main() {
      // Create a file and write content
      using (StreamWriter sw = new StreamWriter("quiz.txt")) {
         sw.WriteLine("Quizzes!");
      }
      
      // Get file information
      FileInfo file = new FileInfo("quiz.txt");
      
      // Get last access time
      DateTime lastAccessTime = file.LastAccessTime;
      Console.WriteLine("Last Access Time: " + lastAccessTime);
      
      // Display other file times for comparison
      Console.WriteLine("Creation Time: " + file.CreationTime);
      Console.WriteLine("Last Write Time: " + file.LastWriteTime);
   }
}

The output of the above code is −

Last Access Time: 12/15/2024 10:30:45 AM
Creation Time: 12/15/2024 10:30:45 AM
Last Write Time: 12/15/2024 10:30:45 AM

Using File.GetLastAccessTime() Method

The static method approach is more direct when you only need the last access time −

using System;
using System.IO;

public class Program {
   public static void Main() {
      string fileName = "sample.txt";
      
      // Create a file
      File.WriteAllText(fileName, "Sample content for testing");
      
      // Get last access time using static method
      DateTime lastAccess = File.GetLastAccessTime(fileName);
      Console.WriteLine("File: " + fileName);
      Console.WriteLine("Last Access Time: " + lastAccess.ToString("yyyy-MM-dd HH:mm:ss"));
      
      // Access the file by reading it
      string content = File.ReadAllText(fileName);
      
      // Check last access time again
      DateTime newLastAccess = File.GetLastAccessTime(fileName);
      Console.WriteLine("After reading file:");
      Console.WriteLine("Last Access Time: " + newLastAccess.ToString("yyyy-MM-dd HH:mm:ss"));
   }
}

The output of the above code is −

File: sample.txt
Last Access Time: 2024-12-15 10:30:45
After reading file:
Last Access Time: 2024-12-15 10:30:45

Comparison of File Time Methods

Method Description Use Case
FileInfo.LastAccessTime Object-oriented approach When you need multiple file properties
File.GetLastAccessTime() Static method approach When you only need the last access time
FileInfo.LastAccessTimeUtc Returns UTC time For time zone independent operations

Working with UTC Time

You can also get the last access time in UTC format to avoid time zone issues −

using System;
using System.IO;

public class Program {
   public static void Main() {
      string fileName = "utc_example.txt";
      
      // Create a file
      File.WriteAllText(fileName, "UTC time example");
      
      // Get times in different formats
      FileInfo fileInfo = new FileInfo(fileName);
      
      Console.WriteLine("Local Time: " + fileInfo.LastAccessTime);
      Console.WriteLine("UTC Time: " + fileInfo.LastAccessTimeUtc);
      Console.WriteLine("Static Method (Local): " + File.GetLastAccessTime(fileName));
      Console.WriteLine("Static Method (UTC): " + File.GetLastAccessTimeUtc(fileName));
   }
}

The output of the above code is −

Local Time: 12/15/2024 10:30:45 AM
UTC Time: 12/15/2024 3:30:45 PM
Static Method (Local): 12/15/2024 10:30:45 AM
Static Method (UTC): 12/15/2024 3:30:45 PM

Conclusion

Getting the last access time of a file in C# can be accomplished using either the FileInfo.LastAccessTime property or the File.GetLastAccessTime() static method. Use FileInfo when you need multiple file properties, and use the static method for simple, direct access to just the last access time.

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

848 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements