Open In App

File.SetCreationTimeUtc() Method in C# with Examples

Last Updated : 09 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

File.SetCreationTimeUtc(String, DateTime) is an inbuilt File class method that is used to set the date and time, in coordinated universal time (UTC), that the file was created.
Syntax: 
 

public static void SetCreationTimeUtc (string path, DateTime creationTimeUtc);

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

  • path: The specified file for which to set the creation date and time information.
  • creationTimeUtc: The UTC 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 creation Time 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.SetCreationTimeUtc(String, DateTime) method.
Program 1: Before running the below code, a file file.txt is created with some contents shown below-
 

file.txt

 

C#




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


Output: 
 

The File Creation Time in UTC 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

 

C#




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


Output: 
 

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads