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


To get the last access time of a file in C#, use the LastAccessTime() method.

For this, use the FileInfo as well as DateTime classes.Create an object of each −

FileInfo file = new FileInfo("new.txt");
DateTime dt = file.CreationTime;
dt = file.LastAccessTime;

Let us see the complete code −

Example

 Live Demo

using System.IO;
using System;
public class Program {
   public static void Main() {
      using (StreamWriter sw = new StreamWriter("quiz.txt")) {
         sw.WriteLine("Quizzes!");
      }
      FileInfo file = new FileInfo("quiz.txt");
      // last access time
      DateTime dt = file.LastAccessTime;
      Console.WriteLine(dt);
   }
}

Output

9/5/2018 5:21:43 AM

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jun-2020

619 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements