VB.Net program to set file creation time

Here, we are going to learn how to set file creation time in VB.Net?
Submitted by Nidhi, on January 08, 2021 [Last updated : March 08, 2023]

Setting the file's creation time in VB.Net

Here, we will use SetCreationTime() method of the File class to set the new creation time of the specified file and then print the old and new file creation time on the console screen.

Program/Source Code:

The source code to set file creation time is given below. The given program is compiled and executed successfully.

VB.Net code to set the file's creation time

'VB.Net program to set file creation time.

Imports System.IO

Module Module1
    Sub Main()
        Try
            Dim D1 As DateTime = File.GetCreationTime("sample.txt")
            Console.WriteLine("Old File Creation Time : " & D1.ToString())

            Dim D2 As DateTime = New DateTime(2017, 12, 25, 19, 45, 10)
            File.SetCreationTime("sample.txt", D2)

            Dim D3 As DateTime = File.GetCreationTime("sample.txt")
            Console.WriteLine("New File Creation Time : " + D3.ToString())

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

Output

Old File Creation Time : 06-01-2021 22:19:11
New File Creation Time : 25-12-2017 19:45:10
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains the Main() function. The Main() function is the entry point for the program. Here, we get the file creation time of "sample.txt" file using GetCreationTime() method and set new file creation time using SetCreationTime(). After that print the old and new file creation time on the console screen.

If the specified file does not exist in the computer system then a file not found exception gets generated and prints the appropriate message on the console screen.

VB.Net File Handling Programs »





Comments and Discussions!

Load comments ↻





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