Export (0) Print
Expand All

Directory.Delete Method

Deletes a directory and its contents.

Overload List

Deletes an empty directory from a specified path.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Sub Delete(String)
[C#] public static void Delete(string);
[C++] public: static void Delete(String*);
[JScript] public static function Delete(String);

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

Supported by the .NET Compact Framework.

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

Example

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

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Delete. For other examples that might be available, see the individual overload topics.
[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.

See Also

Directory Class | Directory Members | System.IO Namespace

Show:
© 2014 Microsoft