Home »
VB.Net »
VB.Net Programs
VB.Net program to delete a specified file
By Nidhi Last Updated : November 11, 2024
Delete a file in VB.Net
Here, we will use the Delete() method of the File class to delete the file from the system.
Program/Source Code:
The source code to delete a specified file is given below. The given program is compiled and executed successfully.
VB.Net code to delete a specified file
'VB.Net program to delete a specified file.
Imports System.IO
Module Module1
Sub Main()
Try
File.Delete("demo.txt")
Console.WriteLine("File deleted successfully")
Catch ex As FileNotFoundException
Console.WriteLine("File does not exist")
End Try
End Sub
End Module
Output
File deleted successfully
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. And, we deleted the "demo.txt" file using Delete() method of the File class.
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 »