1 out of 1 rated this helpful - Rate this topic

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:

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.