VB.Net program to set the last access time of a specified file or directory

Here, we are going to learn how to set the last access time of a specified file or directory in VB.Net?
Submitted by Nidhi, on January 10, 2021 [Last updated : March 08, 2023]

Setting the last access time of a file/directory in VB.Net

Here, we will use SetLastAccessTime() method of the File directory to set the last access time of the specified file or directory.

Program/Source Code:

The source code to set the last access time of a specified file or directory is given below. The given program is compiled and executed successfully.

VB.Net code to set the last access time of a file/directory

'VB.Net program to set the last access time 
'of a specified file.

Imports System.IO

Module Module1
    Sub Main()
        Try
            Dim dt1 As DateTime

            Console.WriteLine("Time before set last access time:")
            dt1 = File.GetLastAccessTime("sample.txt")
            Console.WriteLine("Last Access Time of file(sample.txt) : " & dt1)

            File.SetLastAccessTime("sample.txt", DateTime.Now)

            Console.WriteLine("Time After set last access time:")

            dt1 = File.GetLastAccessTime("sample.txt")
            Console.WriteLine("Last Access Time of file(sample.txt) : " & dt1)

        Catch ex As FileNotFoundException
            Console.WriteLine("File does not exist")
        End Try
    End Sub
End Module

Output

Time before set last access time:
Last Access Time of file(sample.txt) : 09-01-2021 14:35:38
Time After set last access time:
Last Access Time of file(sample.txt) : 09-01-2021 21:13:35
Press any key to continue . . .

Explanation

In the above program, we created the module Module1 that contains the Main() function. The Main() function is the entry point for the program.

Here, we set the last access time of the "sample.txt" file using SetLastAccessTime() method of the File class. Then we printed the updated last access time on the console screen.

VB.Net File Handling Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.