Export (0) Print
Expand All

FileSystem.DeleteDirectory Method (String, DeleteDirectoryOption)

Deletes a directory.

Namespace:  Microsoft.VisualBasic.FileIO
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public static void DeleteDirectory(
	string directory,
	DeleteDirectoryOption onDirectoryNotEmpty
)

Parameters

directory
Type: System.String
Directory to be deleted.
onDirectoryNotEmpty
Type: Microsoft.VisualBasic.FileIO.DeleteDirectoryOption
Specifies what should be done when a directory that is to be deleted contains files or directories. Default is DeleteDirectoryOption.DeleteAllContents.

ExceptionCondition
ArgumentException

The path is a zero-length string, is malformed, contains only white space, or contains invalid characters (including wildcard characters). The path is a device path (starts with \\.\).

ArgumentNullException

directory is Nothing or an empty string.

DirectoryNotFoundException

The directory does not exist or is a file.

IOException

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.

NotSupportedException

The directory name contains a colon (:).

PathTooLongException

The path exceeds the system-defined maximum length.

SecurityException

The user does not have required permissions.

OperationCanceledException

The user cancels the operation or the directory cannot be deleted.

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

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)


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft