Export (0) Print
Expand All

How to: Delete a File in Visual Basic

The DeleteFile method of the My.Computer.FileSystem object allows you to delete a file. Among the options it offers are: whether to send the deleted file to the Recycle Bin, whether to ask the user to confirm that the file should be deleted, and what to do when the user cancels the operation.

To delete a text file

  • Use the DeleteFile method to delete the file. The following code demonstrates how to delete the file named test.txt.

    My.Computer.FileSystem.DeleteFile("C:\test.txt")
    

To delete a text file and ask the user to confirm that the file should be deleted

  • Use the DeleteFile method to delete the file, setting showUI to AllDialogs. The following code demonstrates how to delete the file named test.txt and allow the user to confirm that the file should be deleted.

    My.Computer.FileSystem.DeleteFile("C:\test.txt",
            Microsoft.VisualBasic.FileIO.UIOption.AllDialogs,
            Microsoft.VisualBasic.FileIO.RecycleOption.DeletePermanently,
            Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)
    

To delete a text file and send it to the Recycle Bin

  • Use the DeleteFile method to delete the file, specifying SendToRecycleBin for the recycle parameter. The following code demonstrates how to delete the file named test.txt and send it to the Recycle Bin.

    My.Computer.FileSystem.DeleteFile("C:\test.txt",
    Microsoft.VisualBasic.FileIO.UIOption.AllDialogs,
    Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin)
    

The following conditions may cause an exception:

Show:
© 2014 Microsoft