Delete Method
Collapse the table of content
Expand the table of content

DirectoryInfo.Delete Method ()


Deletes this DirectoryInfo if it is empty.

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

public override void Delete()

Exception Condition

The directory contains a read-only file.


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


The directory is not empty.


The directory is the application's current working directory.


There is an open handle on the directory, and the operating system is Windows XP or earlier. This open handle can result from enumerating directories. For more information, see How to: Enumerate Directories and Files.


The caller does not have the required permission.

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

            // Create the directories.

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


for writing directories. Associated enumeration: FileIOPermissionAccess.Write

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
© 2015 Microsoft