Skip to content
Related Articles
Open in App
Not now

Related Articles

C# Program to Get File Time Using File Class

Improve Article
Save Article
  • Last Updated : 01 Nov, 2021
Improve Article
Save Article

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.


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;

  • UnauthorizedAccessException: This exception occurs when the caller does not have the required permission.
  • ArgumentException: This exception occurs when the user is given an argument of invalid type like zero-length string, contains one or more invalid characters.
  • ArgumentNullException: This exception occurs when the file path is null.
  • PathTooLongException: This exception occurs when the specified filepath, file name, or both exceed the system-defined maximum length.
  • NotSupportedException: This exception will occur when the file path is in an invalid format.


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);


File is created at: 10/22/2021 1:02:10 PM
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!