This documentation is archived and is not being maintained.

Directory.Delete Method (String, Boolean)

.NET Framework 1.1

Deletes the specified directory and, if indicated, any subdirectories in the directory.

[Visual Basic]
Overloads Public Shared Sub Delete( _
   ByVal path As String, _
   ByVal recursive As Boolean _
)
[C#]
public static void Delete(
 string path,
 bool recursive
);
[C++]
public: static void Delete(
 String* path,
 bool recursive
);
[JScript]
public static function Delete(
   path : String,
 recursive : Boolean
);

Parameters

path
The name of the directory to remove.
recursive
true to remove directories, subdirectories, and files in path; otherwise, false.

Exceptions

Exception Type Condition
IOException The directory specified by path is read-only, or recursive is false and path is not an empty directory.
UnauthorizedAccessException The caller does not have the required permission.
ArgumentException path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.
ArgumentNullException path is a null reference (Nothing in Visual Basic).
PathTooLongException The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
DirectoryNotFoundException The specified path is invalid, such as being on an unmapped drive.

Remarks

The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

The path parameter is not case-sensitive.

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Copy a directory. Directory
Rename or move a directory. Directory.Move

DirectoryInfo.MoveTo

Delete a file. File.Delete

FileInfo.Delete

Create a directory. CreateDirectory

Directory

Create a subdirectory. CreateSubdirectory
See the files in a directory. Name
See the subdirectories of a directory. GetDirectories

GetDirectories

See all the files in all subdirectories of a directory. GetFileSystemInfos

.NET Compact Framework Platform Note:  Some device file systems do not support relative paths. Specify absolute path information.

Example

[Visual Basic, C#, C++] The following example creates and deletes the specified directory and subdirectories.

[Visual Basic] 
Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim path As String = "c:\MyDir"
        Dim subPath As String = "c:\MyDir\temp"

        Try
            ' Determine whether the directory exists.
            If Directory.Exists(path) = False Then
                ' Create the directory.
                Directory.CreateDirectory(path)
            End If

            ' Determine whether the directory exists.
            If Directory.Exists(subPath) = False Then
                ' Create the directory.
                Directory.CreateDirectory(subPath)
            End If

            'This operation will not be allowed because there are subdirectories.
            Console.WriteLine("I am about to attempt to delete {0}", path)
            Directory.Delete(path, True)
            Console.WriteLine("The Delete operation was successful.")

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

[C#] 
using System;
using System.IO;

class Test 
{
    
    public static void Main() 
    {
        // Specify the directories you want to manipulate.
        string path = @"c:\MyDir";
        string subPath = @"c:\MyDir\temp";

        try 
        {
            // Determine whether the directory exists.
            if (!Directory.Exists(path)) 
            {
                // Create the directory.
                Directory.CreateDirectory(path);
            }


            if (!Directory.Exists(subPath)) 
            {
                // Create the directory.
                Directory.CreateDirectory(subPath);
            }

            // This will succeed because subdirectories are being deleted.
            Console.WriteLine("I am about to attempt to delete {0}", path);
            Directory.Delete(path, true);
            Console.WriteLine("The Delete operation was successful.");

        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        } 
        finally {}
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    // Specify the directories you want to manipulate.
    String* path = S"c:\\MyDir";
    String* subPath = S"c:\\MyDir\\temp";

    try {
        // Determine whether the directory exists.
        if (!Directory::Exists(path)) {
            // Create the directory.
            Directory::CreateDirectory(path);
        }

        if (!Directory::Exists(subPath)) {
            // Create the directory.
            Directory::CreateDirectory(subPath);
        }

        // This will succeed because subdirectories are being deleted.
        Console::WriteLine(S"I am about to attempt to delete {0}", path);
        Directory::Delete(path, true);
        Console::WriteLine(S"The Delete operation was successful.");
    } catch (Exception* e) {
        Console::WriteLine(S"The process failed: {0}", e);
    } 
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Directory Class | Directory Members | System.IO Namespace | Directory.Delete Overload List | Working with I/O | Reading Text from a File | Writing Text to a File

Show: