DirectoryInfo.Delete Method ()
Deletes this DirectoryInfo if it is empty.
Assembly: mscorlib (in mscorlib.dll)
| Exception | Condition |
|---|---|
| UnauthorizedAccessException | The directory contains a read-only file. |
| DirectoryNotFoundException | The directory described by this DirectoryInfo object does not exist or could not be found. |
| IOException | The directory is not empty. -or- The directory is the application's current working directory. -or- 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. |
| SecurityException | 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"); 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 {} } }
for writing directories. Associated enumeration: FileIOPermissionAccess.Write
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0