How to: Delete All Files in a Directory in Visual Basic 
This page is specific to:.NET Framework Version:2.03.54.0
Visual Basic Language Concepts 
How to: Delete All Files in a Directory 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 all of the files in a folder

  1. Use the My.Computer.FileSystem.GetFiles method to return the collection of strings representing the files in the directory.

  2. Use a For…Each loop with the DeleteFile method to delete each file in turn.

    The following example deletes all files in the My Documents folder.

    For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
        My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
        FileIO.SearchOption.SearchAllSubDirectories, "*.*")
    
        My.Computer.FileSystem.DeleteFile(foundFile, _
            FileIO.UIOption.AllDialogs, _
            FileIO.RecycleOption.DeletePermanently)
    Next
    
    

Robust Programming

The following conditions may cause an exception:

See Also

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View