File.SetCreationTime() Method in C# with Examples

File.SetCreationTime(String, DateTime) is an inbuilt File class method that is used to set the local date and time the file was created.

Syntax:

public static void SetCreationTime (string path, DateTime creationTime);

Parameter: This function accepts two parameters which are illustrated below:

  • path: The specified file for which to set the creation date and time information.
  • creationTime: The local Date and time containing the value to set for the creation of path.

Exceptions:

  • FileNotFoundException: The specified path was not found.
  • ArgumentException: The path is a zero-length string, contains only white space, or one or more invalid characters as defined by InvalidPathChars.
  • ArgumentNullException: The path is null.
  • PathTooLongException: The specified path, file name, or both exceed the system-defined maximum length.
  • IOException: An I/O error occurred while performing the operation.
  • ArgumentOutOfRangeException: The creationTime specifies a value outside the range of dates, times, or both permitted for this operation.
  • UnauthorizedAccessException: The caller does not have the required permission.
  • NotSupportedException: The path is in an invalid format.

Below are the programs to illustrate the File.SetCreationTime(String, DateTime) method.



Program 1: Before running the below code, a file file.txt is created with some contents shown below-

file.txt

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the usage
// of File.SetCreationTime(String, DateTime) method
  
// Using System and System.IO namespaces
using System;
using System.IO;
  
class GFG {
    static void Main()
    {
        // Specifing a new date and time
        DateTime D1 = new DateTime(2017, 12, 25, 2, 6, 8);
  
        // Calling SetCreationTime() function
        // to set the new date and time
        File.SetCreationTime("file.txt", D1);
  
        // Calling the GetCreationTime() function
        // to get the creation date and time
        DateTime D2 = File.GetCreationTime("file.txt");
        Console.WriteLine("The File Creation Time is : " + D2.ToString());
    }
}

chevron_right


Output:

The File Creation Time is : 25/12/2017 02:06:08 AM

Program 2: Before running the below code, a file file.txt is created with some contents shown below:

file.txt

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the usage
// of File.SetCreationTime(String, DateTime) method
  
// Using System and System.IO namespaces
using System;
using System.IO;
  
class GFG {
    static void Main()
    {
        // Calling the GetCreationTime() function to
        // get the original file creation date and time
        DateTime D1 = File.GetCreationTime("file.txt");
        Console.WriteLine("File Creation Old Time is: " + D1.ToString());
  
        // Specifying a new date and time
        DateTime D2 = new DateTime(2017, 12, 25, 2, 6, 8);
  
        // Calling SetCreationTime() function
        // to set the new date and time
        File.SetCreationTime("file.txt", D2);
  
        // Calling the GetCreationTime() function
        // to get the new creation date and time
        DateTime D3 = File.GetCreationTime("file.txt");
        Console.WriteLine("File Creation new Time is : " + D3.ToString());
    }
}

chevron_right


Output:

File Creation Old Time is: 4/25/2020 11:26:14 AM
File Creation new Time is : 25/12/2017 02:06:08 AM



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.