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.
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.
- 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-
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 Creation Old Time is: 4/25/2020 11:26:14 AM File Creation new Time is : 25/12/2017 02:06:08 AM