VB.Net - How to Delete File And Folder Using Visual Basic.Net
In This VB.Net Tutorial We Will See How To Delete File Or A Folder From A Path String Using Visual Basic.Net Programming Language.
Project Source Code:
Imports System.IO
Public Class Remove_File_Folder
' remove file if exists
Private Sub ButtonDeleteFile_Click(sender As Object, e As EventArgs) Handles ButtonDeleteFile.Click
Dim filePath As String
filePath = TextBoxFilePath.Text
If File.Exists(filePath) Then
File.Delete(filePath)
Else
MessageBox.Show("File Not Found")
End If
End Sub
' remove directory if exists
Private Sub ButtonDeleteFolder_Click(sender As Object, e As EventArgs) Handles ButtonDeleteFolder.Click
Dim folderPath As String
folderPath = TextBoxFolderPath.Text
If Directory.Exists(folderPath) Then
'if the directory contain files
Directory.Delete(folderPath, True)
Else
MessageBox.Show("Folder Not Found")
End If
End Sub
End Class