Export (0) Print
Expand All

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

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.

The following table lists examples of other typical or related I/O tasks.

To do this...

See the example in this topic...

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 and all the subdirectories in a directory.

GetFileSystemInfos

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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft