Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

File.SetLastAccessTime() Method in C# with Examples

  • Last Updated : 09 Nov, 2021

File.SetLastAccessTime(String, DateTime) is an inbuilt File class method that is used to set the date and time the specified file was last accessed.

Syntax: 

public static void SetLastAccessTime (string path, DateTime lastAccessTime);

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

  • path: This is the file for which to set the access date and time information.
  • lastAccessTime: This is the specified last access local date and 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 lastAccessTime specifies a value outside the range of dates or times permitted for this operation.

Below are the programs to illustrate the File.SetLastAccessTime(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.SetLastAccessTime() 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 SetLastAccessTime() function
        // to set last access date and time
        File.SetLastAccessTime(myfile, new DateTime(2020, 5, 4, 4, 5, 7));
 
        // Getting the last access date and time
        DateTime dt = File.GetLastAccessTime(myfile);
        Console.WriteLine("The last access date and"+
                 " time for this file was {0}.", dt);
    }
}

Output: 

The last access date and time 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 access date and time. 

C#




// C# program to illustrate the usage
// of File.SetLastAccessTime() 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 SetLastAccessTime() function
        // to set last access date and time
        File.SetLastAccessTime(path, new DateTime(2019, 5, 4, 4, 5, 7));
 
        // Getting the last access date and time
        DateTime dt = File.GetLastAccessTime(path);
        Console.WriteLine("The last access date and "+
                  "time for this file was {0}.", dt);
    }
}

Output: 

The last access date and time 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!