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

CsharpProgrammingServer Side Programming

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

For this, use the FileInfo as well as DateTime classes.

Create an object of each −

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

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("amit.txt")) {
         sw.WriteLine("Welcome!");
      }
      FileInfo file = new FileInfo("amit.txt");
      // file creation time
      DateTime dt = file.CreationTime;
      Console.WriteLine(dt);
      // last access time
      dt = file.LastAccessTime;
      Console.WriteLine(dt);
      // last write time
      dt = file.LastWriteTime;
      Console.WriteLine(dt);
   }
}

Output

9/5/2018 5:17:27 AM
9/5/2018 5:17:27 AM
9/5/2018 5:17:27 AM
raja
Published on 05-Sep-2018 08:13:01
Advertisements