Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
1 out of 1 rated this helpful - Rate this topic

DirectoryInfo.Delete Method ()

.NET Framework 1.1

Deletes this DirectoryInfo if it is empty.

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

Exceptions

Exception Type Condition
IOException The directory is not empty.
SecurityException The caller does not have the required permission.

Remarks

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

[Visual Basic, C#, C++] The following example throws an exception if you attempt to delete a directory that is not empty.

[Visual Basic] 
Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim di1 As DirectoryInfo = New DirectoryInfo("c:\MyDir")

        Try
            ' Create the directories.
            di1.Create()
            di1.CreateSubdirectory("temp")

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

        Catch
            Console.WriteLine("The Delete operation was unsuccessful, as expected.")
        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.
        DirectoryInfo di1 = new DirectoryInfo(@"c:\MyDir");

        try 
        {
            // Create the directories.
            di1.Create();
            di1.CreateSubdirectory("temp");

            //This operation will not be allowed because there are subdirectories.
            Console.WriteLine("I am about to attempt to delete {0}", di1.Name);
            di1.Delete();
            Console.WriteLine("The Delete operation was successful, which was unexpected.");
        } 
        catch (Exception) 
        {
            Console.WriteLine("The Delete operation failed as expected.");
        } 
        finally {}
    }
}

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

using namespace System;
using namespace System::IO;

int main() {
    // Specify the directories you want to manipulate.
    DirectoryInfo* di1 = new DirectoryInfo(S"c:\\MyDir");

    try {
        // Create the directories.
        di1->Create();
        di1->CreateSubdirectory(S"temp");

        //This operation will not be allowed because there are subdirectories.
        Console::WriteLine(S"I am about to attempt to delete {0}", di1->Name);
        di1->Delete();
        Console::WriteLine(S"The Delete operation was successful, which was unexpected.");
    } catch (Exception*) {
        Console::WriteLine(S"The Delete operation failed as expected.");
    } 
}

[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

.NET Framework Security: 

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.