DirectoryInfo.Delete Method ()
.NET Framework 3.0
Deletes this DirectoryInfo if it is empty.
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
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
- FileIOPermission for writing directories. Associated enumeration: FileIOPermissionAccess.Write
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.