Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

File.SetLastWriteTimeUtc() Method in C# with Examples

  • Last Updated : 13 Jan, 2022

File.SetLastWriteTimeUtc(String) is an inbuilt File class method which is used to set the date and time, in coordinated universal time (UTC), that the specified file was last written to.
Syntax:  

public static void SetLastWriteTimeUtc (string path, DateTime lastWriteTime);

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

  • path: This is the specified file for which to set the date and time information.
  • lastWriteTime: This is the specified last written date and time of the path that is expressed in UTC time.

Exceptions: 

  • 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.
  • FileNotFoundException: The specified path was not found.
  • UnauthorizedAccessException: The caller does not have the required permission.
  • NotSupportedException: The path is in an invalid format.
  • ArgumentOutOfRangeException: The lastWriteTime specifies a value outside the range of dates or times permitted for this operation.

Below are the programs to illustrate the File.SetLastWriteTimeUtc(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.SetLastWriteTimeUtc() method
 
// Using System and System.IO namespaces
using System;
using System.IO;
 
class GFG {
    public static void Main()
    {
        // Specifying a file
        string myfile = @"file.txt";
 
        // Calling the SetLastWriteTimeUtc() function
        // to set last written date and time in UTC
        File.SetLastWriteTimeUtc(myfile, new DateTime(2020,
                                           5, 4, 4, 5, 7));
 
        // Getting the last written date and time
        // expressed in UTC of the file
        DateTime dt = File.GetLastWriteTimeUtc(myfile);
        Console.WriteLine("The last written date and"+
           " time in UTC for this file was {0}.", dt);
    }
}

Output: 

The last written date and time in UTC for this file was 5/4/2020 4:05:07 AM.

Program 2: Initially, no file was created. Below code, itself creates a file file.txt and prints the last written date and time.

C#




// C# program to illustrate the usage
// of File.SetLastWriteTimeUtc() method
 
// Using System and System.IO namespaces
using System;
using System.IO;
 
class GFG {
    public static void Main()
    {
        // Specifying a file
        string path = @"file.txt";
 
        // Checking the existence of the file
        if (!File.Exists(path)) {
            File.Create(path);
        }
 
        // Calling the SetLastWriteTimeUtc() function
        // to set last written date and time in UTC
        File.SetLastWriteTimeUtc(path, new DateTime(2019,
                                         5, 4, 4, 5, 7));
 
        // Getting the last written date and time in UTC
        // of the specified file
        DateTime dt = File.GetLastWriteTimeUtc(path);
        Console.WriteLine("The last written date and time "+
                       "in UTC for this file was {0}.", dt);
    }
}

Executing: 

The last written date and time in UTC for this file was 5/4/2019 4:05:07 AM.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!