Export (0) Print
Expand All

DirectoryInfo.Delete Method (Boolean)

.NET Framework 1.1

Deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.

[Visual Basic]
Overloads Public Sub Delete( _
   ByVal recursive As Boolean _
)
[C#]
public void Delete(
 bool recursive
);
[C++]
public: void Delete(
 bool recursive
);
[JScript]
public function Delete(
   recursive : Boolean
);

Parameters

recursive
true to delete this directory, its subdirectories, and all files; otherwise, false.

Exceptions

Exception Type Condition
IOException The directory is read-only.

-or-

The directory contains one or more files or subdirectories and recursive is false.

SecurityException The caller does not have the required permission.

Remarks

If the DirectoryInfo has no files or subdirectories, this method deletes the DirectoryInfo even if recursive is false. Attempting to delete a DirectoryInfo that is not empty when recursive is false throws an IOException.

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

Example

The following example demonstrates deleting a directory. Because the directory is removed, first comment out the Delete line to test that the directory exists. Then uncomment the same line of code to test that the directory was removed successfully.

[Visual Basic] 
Imports System
Imports System.IO

Public Class DeleteTest

    Public Shared Sub Main()
        ' Make a reference to a directory.
        Dim di As New DirectoryInfo("TempDir")

        ' Create the directory only if it does not already exist.
        If di.Exists = False Then
            di.Create()
        End If

        Dim dis As DirectoryInfo = di.CreateSubdirectory("SubDir")
        ' Create a subdirectory in the directory just created.

        ' Process that directory as required.
        ' ...

        ' Delete the subdirectory. The true indicates that if subdirectories
        ' or files are in this directory, they are to be deleted as well.
        dis.Delete(True)

        ' Delete the directory.
        di.Delete(True)
    End Sub 'Main
End Class 'DeleteTest

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

public class DeleteTest 
{
    public static void Main() 
    {

        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (di.Exists == false)
            di.Create();

        // Create a subdirectory in the directory just created.
        DirectoryInfo dis = di.CreateSubdirectory("SubDir");

        // Process that directory as required.
        // ...

        // Delete the subdirectory. The true indicates that if subdirectories
        // or files are in this directory, they are to be deleted as well.
        dis.Delete(true);

        // Delete the directory.
        di.Delete(true);
    }
}

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

using namespace System;
using namespace System::IO;

int main() {
    // Make a reference to a directory.
    DirectoryInfo* di = new DirectoryInfo(S"TempDir");

    // Create the directory only if it does not already exist.
    if (!di->Exists)
        di->Create();

    // Create a subdirectory in the directory just created.
    DirectoryInfo* dis = di->CreateSubdirectory(S"SubDir");

    // Process that directory as required.
    // ...

    // Delete the subdirectory. The true indicates that if subdirectories
    // or files are in this directory, they are to be deleted as well.
    dis->Delete(true);

    // Delete the directory.
    di->Delete(true);
}

[JScript] 
import System;
import System.IO;

public class DeleteTest {
    public static function Main() {

        // Make a reference to a directory.
        var di : DirectoryInfo = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (di.Exists == false)
            di.Create();

        // Create a subdirectory in the directory just created.
        var dis : DirectoryInfo = di.CreateSubdirectory("SubDir");

        // Process that directory as required.
        // ...

        // Delete the subdirectory. The true indicates that if subdirectories
        // or files are in this directory, they are to be deleted as well.
        dis.Delete(true);

        // Delete the directory.
        di.Delete(true);
    }
}
DeleteTest.Main();

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

.NET Framework Security: 

  • FileIOPermissionAccess Write flag required for write permission to the DirectoryInfo and subdirectories to be deleted.

See Also

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

Show:
© 2014 Microsoft