Udostępnij za pośrednictwem


My.Computer.FileSystem.DeleteDirectory Method

Deletes a directory.

' Usage
My.Computer.FileSystem.DeleteDirectory(directory ,onDirectoryNotEmpty)
My.Computer.FileSystem.DeleteDirectory(directory ,showUI ,recycle)
My.Computer.FileSystem.DeleteDirectory(directory ,showUI ,recycle ,onUserCancel)
' Declaration
Public Sub DeleteDirectory( _
   ByVal directory As String, _
   ByVal onDirectoryNotEmpty As DeleteDirectoryOption _
)
' -or-
Public Sub DeleteDirectory( _
   ByVal directory As String, _
   ByVal showUI As UIOption, _
   ByVal recycle As RecycleOption _
)
' -or-
Public Sub DeleteDirectory( _
   ByVal directory As String, _
   ByVal showUI As UIOption, _
   ByVal recycle As RecycleOption, _
   ByVal onUserCancel As UICancelOption _
)

Parameters

  • directory
    String. Directory to be deleted. Required.

  • onDirectoryNotEmpty
    DeleteDirectoryOption Enumeration. Specifies what should be done when a directory that is to be deleted contains files or directories. Default is DeleteDirectoryOption.DeleteAllContents.

  • showUI
    UIOption Enumeration. Specifies whether to visually track the operation's progress. Default is UIOption.OnlyErrorDialogs. Required.

  • recycle
    RecycleOption Enumeration. Specifies whether or not the deleted file should be sent to the Recycle Bin. Default is RecycleOption.DeletePermanently.

  • onUserCancel
    UICancelOption Enumeration. Specifies whether to throw an exception if the user clicks Cancel. Required.

Exceptions

The following conditions may cause an exception:

Remarks

The showUI, recycle, and onUserCancel parameters are not supported in applications that are not user interactive, such as Windows Services.

Tasks

The following table lists an example of a task involving the My.Computer.FileSystem.DeleteDirectory method.

To

See

Delete a directory

How to: Delete a Directory in Visual Basic

Example

The example deletes the directory OldDirectory only if it is empty.

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

This example deletes the directory OldDirectory and all of its contents.

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

This example deletes the directory OldDirectory and all of its contents, asking the user to confirm the deletion, but does not send the contents to the Recycle Bin.

My.Computer.FileSystem.DeleteDirectory _
("C:\OldDirectory", FileIO.UIOption.AllDialogs, FileIO.RecycleOption.DeletePermanently, FileIO.UICancelOption.ThrowException)

This example deletes the directory OldDirectory and all of its contents, sending them to the Recycle Bin, but does not show the progress of the operation.

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

Requirements

Namespace:Microsoft.VisualBasic.MyServices

Class:FileSystemProxy (provides access to FileSystem)

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Availability by Project Type

Project type

Available

Windows Application

Yes

Class Library

Yes

Console Application

Yes

Windows Control Library

Yes

Web Control Library

Yes

Windows Service

Yes

Web Site

Yes

Permissions

The following permissions may be necessary:

Permission

Description

FileIOPermission

Controls the ability to access files and folders. Associated enumeration: Unrestricted.

UIPermission

Controls the permissions related to user interfaces and the clipboard. Associated enumeration: SafeSubWindows.

For more information, see Code Access Security and Requesting Permissions.

See Also

Tasks

How to: Delete a File in Visual Basic

How to: Delete a Directory in Visual Basic

Reference

My.Computer.FileSystem Object

UICancelOption Enumeration

FileSystem.DeleteDirectory

UICancelOption

RecycleOption Enumeration

DeleteDirectoryOption Enumeration

UICancelOption Enumeration

UIOption Enumeration