Export (0) Print
Expand All

DirectoryInfo.Delete Method

Deletes a DirectoryInfo and its contents from a path.

Overload List

Deletes this DirectoryInfo if it is empty.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Overrides Public Sub Delete()
[C#] public override void Delete();
[C++] public: void Delete();
[JScript] public override function Delete();

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

Supported by the .NET Compact Framework.

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

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();

See Also

DirectoryInfo Class | DirectoryInfo Members | System.IO Namespace

Show:
© 2014 Microsoft