C# - How to Get File's Creation Time?

learn how to print (get) the file creation time using C# program?
Submitted by IncludeHelp, on October 29, 2017 [Last updated : March 26, 2023]

Here, we have to create a file and print the time, when it is created.

To get file's creation time in C#, we use File.GetCreationTime() method.

File.GetCreationTime()

This is a method of "File" class, which returns the file creation time.

Syntax

DateTime GetCreationTime(path);

Parameter(s)

path - Filename with its location.

Return Value

It returns a DateTime object. This contains following information:

  1. Month
  2. Date
  3. Year
  4. Hour
  5. Minutes
  6. Seconds
  7. AM and PM

C# program to get file creation time

using System;
using System.IO;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      DateTime D1 = File.GetCreationTime("XYZ.TXT");
      Console.WriteLine("File Creation Time : " + D1.ToString());
    }
  }
}

Output

File Creation Time : 10/27/2017 6:53:39 PM

Explanation

In above program, we need to remember, when we use "File" class, System.IO namespace must be included in the program.

C# File Handling Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.