Open In App

C# Program to Get File Time Using File Class

Given a file, now our task is to get the file time using the File class. So we use the GetCreationTime() method of the File class. This method is used to find the creation date and time of the given file or directory. This method will only take one parameter that is the path of the file and if this path parameter does not exist, then it will return 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adjusted to local time.

Syntax:



public static DateTime GetCreationTime (string Ipath)

Here Ipath represents the path of the file or directory. 



Return Type: The return type of this method is DateTime. It is a structure set to the date and time that the specified file.

Exceptions: It can have the following exceptions;

Example:

In this example, we are going to create a file named “file.txt” in the C drive, and the path is shown in the image:





// C# program to get file time
// using File Class
using System;
using System.IO;
  
class GFG{
  
static void Main()
{
      
    // Declaring a time variable that will store 
    // the creation time of the file 
    // Using GetCreationTime() method of File class
    DateTime createdtime = File.GetCreationTime("C://users//file.txt");
      
    // Display the creation time of the file
    Console.WriteLine("File is created at: {0}", createdtime);
}
}

Output:

File is created at: 10/22/2021 1:02:10 PM
Article Tags :
C#