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.

DirectoryInfo.Delete Method ()

Deletes this DirectoryInfo if it is empty.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

public override void Delete ()
public void Delete ()
public override function Delete ()
Not applicable.

Exception typeCondition

IOException

The directory is not empty.

-or-

The directory is the application's current working directory.

SecurityException

The caller does not have the required permission.

DirectoryNotFoundException

The directory described by this DirectoryInfo object does not exist or could not be found.

For a list of common I/O tasks, see Common I/O Tasks.

The following example throws an exception if you attempt to delete a directory that is not empty.

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 {}
    }
}

import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        // 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.get_Name());
            di1.Delete();
            Console.WriteLine("The Delete operation was successful, "
                + "which was unexpected.");
        }
        catch (System.Exception exp) {
            Console.WriteLine("The Delete operation failed as expected.");
        }
        finally {
        }
    } //main
} //Test

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft