Open In App

File.GetLastWriteTimeUtc() Method in C# with Examples

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

public static DateTime GetLastWriteTimeUtc (string path);

Parameter: This function accepts a parameter which is illustrated below: 
 



  • path: This is the specified file path.

Exceptions:
 

Return Value: Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.
Below are the programs to illustrate the File.GetLastWriteTimeUtc(String) method.
Program 1: Before running the below code, a file file.txt is created with some contents shown below:
 



 




// C# program to illustrate the usage
// of File.GetLastWriteTimeUtc(String) 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 GetLastWriteTimeUtc() function
        DateTime dt = File.GetLastWriteTimeUtc(myfile);
 
        // Getting the last write time in UTC
        Console.WriteLine("The last write time in "+
                  "UTC for this file was {0}.", dt);
    }
}

Executing: 
 

The last write time in UTC for this file was 4/3/2020 12:00:00 AM.

Program 2: Before running the below code, a file was created shown below:
 

 




// C# program to illustrate the usage
// of File.GetLastWriteTimeUtc(String) 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";
 
        // Setting the date
        File.SetLastWriteTime(myfile, new DateTime(2020, 4, 3));
 
        // Calling GetLastWriteTimeUtc() function
        DateTime dt = File.GetLastWriteTimeUtc(myfile);
 
        // Getting the last write time in UTC
        Console.WriteLine("The last write time in"+
                " UTC for this file was {0}.", dt);
    }
}

Executing: 
 

The last write time in UTC for this file was 4/3/2020 12:00:00 AM.

 


Article Tags :
C#