Click to Rate and Give Feedback

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Basic Language Concepts 
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.

    Visual Basic
    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.

    Visual Basic
    My.Computer.FileSystem.DeleteFile("C:\test.txt", _
            FileIO.UIOption.AllDialogs, FileIO.RecycleOption.DeletePermanently, 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.

    Visual Basic
    My.Computer.FileSystem.DeleteFile("C:\test.txt", _
    FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin)
    

Robust Programming

The following conditions may cause an exception:

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
How to delete some files from a directory      CalvinH   |   Edit   |   Show History

If you want to use wildcards to delete a file, you can create a local array of files and delete each one:

        For Each tmpfile As String In IO.Directory.GetFiles(cDataPath, "t*.mht")
            My.Computer.FileSystem.DeleteFile(tmpfile)
        Next

Tags What's this?: Add a tag
Flag as ContentBug
Why not using a more 2.0 way      Ran   |   Edit   |   Show History
string[] filesToDelete = Directory.GetFiles(Path, Pattern);
Array.ForEach<string>(filesToDelete, File.Delete);
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker