Get the creation time of a file in C#


To get the creation time of a file in C#, use the CreationTime() 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;

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("qa.txt")) {
         sw.WriteLine("Questions and Answers!");
      }
      FileInfo file = new FileInfo("qa.txt");
      // file creation time
      DateTime dt = file.CreationTime;
      Console.WriteLine(dt);
   }
}

Output

9/5/2018 5:20:03 AM

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 22-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements