Bagikan melalui


How to: Delete a Directory in Visual Basic

Use the DeleteDirectory method of the My.Computer.FileSystem object to delete a directory. Among the options are: whether or not to delete the directory's contents, whether or not to send the deleted directory to the Recycle Bin, and whether or not to show the progress of the deletion.

To delete a directory only if it is empty

  • Use the DeleteDirectory method to delete the directory, specifying False for onDirectoryNotEmpty. This example deletes the directory named OldDirectory only if it is empty.

    My.Computer.FileSystem.DeleteDirectory("C:\OldDirectory", _
    FileIO.DeleteDirectoryOption.ThrowIfDirectoryNonEmpty)
    

To delete a directory and send it to the Recycle Bin

  • Use the DeleteDirectory method to delete the directory, specifying RecycleOption.SendToRecycleBin for recycle. This example deletes the directory named OldDirectory and all of its contents, sending them to the Recycle Bin and showing the operation progress.

    My.Computer.FileSystem.DeleteDirectory("C:\OldDirectory", FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin)
    

Robust Programming

The following conditions may cause an exception:

See Also

Tasks

How to: Delete a File in Visual Basic

Reference

UICancelOption Enumeration

My.Computer.FileSystem.DeleteDirectory Method

RecycleOption Enumeration

UIOption Enumeration

DeleteDirectoryOption Enumeration