Export (0) Print
Expand All
Expand Minimize

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.

The following conditions may cause an exception:

  • The path is a zero-length string, is malformed, contains only white space, or contains invalid characters (including wildcard characters) (ArgumentException).

  • The path is a device path (starts with \\.\) (ArgumentException).

  • The path is Nothing (ArgumentNullException).

  • The directory does not exist or is a file (DirectoryNotFoundException).

  • The directory is not empty, and onDirectoryNotEmpty is set to ThrowIfDirectoryNonEmpty (IOException).

  • The user does not have permission to delete the directory or subdirectory (IOException).

  • A file in the directory or subdirectory is in use (IOException).

  • A file or directory name contains a colon (:) (NotSupportedException).

  • The user cancels the operation or the directory cannot be deleted (OperationCanceledException).

  • The path exceeds the system-defined maximum length (PathTooLongException).

  • The user does not have required permissions (SecurityException).

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

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

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)

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

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.

Community Additions

ADD
Show:
© 2014 Microsoft