DirectoryInfo::Delete Method ()

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Deletes this DirectoryInfo if it is empty.

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

public:
virtual void Delete() override

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 namespace System;
using namespace System::IO;
int main()
{

   // Specify the directories you want to manipulate.
   DirectoryInfo^ di1 = gcnew 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." );
   }

}

FileIOPermission

for writing directories. Associated enumeration: FileIOPermissionAccess::Write

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show: