File.GetLastAccessTimeUtc() Method in C# with Examples

File.GetLastAccessTimeUtc(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 accessed.

Syntax:

public static DateTime GetLastAccessTimeUtc (string path);

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

  • path: This is the specified file path.

Exceptions:

  • UnauthorizedAccessException: The caller does not have the required permission.
  • 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 given path, file name, or both exceed the system-defined maximum length.
  • NotSupportedException: The path is in an invalid format.

Return Value: Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last accessed.



Below are the programs to illustrate the File.GetLastAccessTimeUtc(String) 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.GetLastAccessTimeUtc(String) method
  
// Using System and System.IO namespaces
using System;
using System.IO;
  
class GFG {
    public static void Main()
    {
        // Specifing a file
        string myfile = @"file.txt";
  
        // Calling GetLastAccessTimeUtc() function
        DateTime dt = File.GetLastAccessTimeUtc(myfile);
  
        // Getting the last access time
        Console.WriteLine("The last access time in UTC for this file was {0}.", dt);
    }
}

chevron_right


Executing:

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

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

file.txt

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the usage
// of File.GetLastAccessTimeUtc(String) method
  
// Using System and System.IO namespaces
using System;
using System.IO;
  
class GFG {
    public static void Main()
    {
        // Specifing a file
        string myfile = @"file.txt";
  
        // Setting the date
        File.SetLastAccessTimeUtc(myfile, new DateTime(2020, 5, 4));
  
        // Calling GetLastAccessTimeUtc() function
        DateTime dt = File.GetLastAccessTimeUtc(myfile);
  
        // Getting the last access time
        Console.WriteLine("The last access time in UTC for this file was {0}.", dt);
    }
}

chevron_right


Executing:

The last access time in UTC for this file was 5/4/2020 12:00:00 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.