ערוך

שתף באמצעות


DirectoryInfo.Delete Method

Definition

Deletes a DirectoryInfo and its contents from a path.

Overloads

Delete()

Deletes this DirectoryInfo if it is empty.

Delete(Boolean)

Deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.

Delete()

Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs

Deletes this DirectoryInfo if it is empty.

public:
 override void Delete();
public override void Delete ();
override this.Delete : unit -> unit
Public Overrides Sub Delete ()

Exceptions

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.

-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.

The caller does not have the required permission.

Examples

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

}
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 {}
    }
}
open System.IO

// Specify the directories you want to manipulate.
let di1 = DirectoryInfo @"c:\MyDir"

try
    // Create the directories.
    di1.Create()
    di1.CreateSubdirectory "temp" |> ignore

    //This operation will not be allowed because there are subdirectories.
    printfn $"I am about to attempt to delete {di1.Name}"
    di1.Delete()
    printfn "The Delete operation was successful, which was unexpected."
with _ ->
    printfn "The Delete operation failed as expected."
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim di1 As DirectoryInfo = 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
            Console.WriteLine("The Delete operation was unsuccessful, as expected.")
        End Try
    End Sub
End Class

Remarks

For a list of common I/O tasks, see Common I/O Tasks.

See also

Applies to

Delete(Boolean)

Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs

Deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.

public:
 void Delete(bool recursive);
public void Delete (bool recursive);
override this.Delete : bool -> unit
Public Sub Delete (recursive As Boolean)

Parameters

recursive
Boolean

true to delete this directory, its subdirectories, and all files; otherwise, false.

Exceptions

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 read-only.

-or-

The directory contains one or more files or subdirectories and recursive is false.

-or-

The directory is the application's current working directory.

-or-

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

The caller does not have the required permission.

Examples

The following example demonstrates deleting a directory. Because the directory is removed, first comment out the Delete line to test that the directory exists. Then uncomment the same line of code to test that the directory was removed successfully.

using namespace System;
using namespace System::IO;
int main()
{
   
   // Make a reference to a directory.
   DirectoryInfo^ di = gcnew DirectoryInfo( "TempDir" );
   
   // Create the directory only if it does not already exist.
   if (  !di->Exists )
      di->Create();

   
   // Create a subdirectory in the directory just created.
   DirectoryInfo^ dis = di->CreateSubdirectory( "SubDir" );
   
   // Process that directory as required.
   // ...
   // Delete the subdirectory. The true indicates that if subdirectories
   // or files are in this directory, they are to be deleted as well.
   dis->Delete( true );
   
   // Delete the directory.
   di->Delete( true );
}
using System;
using System.IO;

public class DeleteTest
{
    public static void Main()
    {

        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (di.Exists == false)
            di.Create();

        // Create a subdirectory in the directory just created.
        DirectoryInfo dis = di.CreateSubdirectory("SubDir");

        // Process that directory as required.
        // ...

        // Delete the subdirectory. The true indicates that if subdirectories
        // or files are in this directory, they are to be deleted as well.
        dis.Delete(true);

        // Delete the directory.
        di.Delete(true);
    }
}
open System.IO

// Make a reference to a directory.
let di = DirectoryInfo "TempDir"

// Create the directory only if it does not already exist.
if not di.Exists then
    di.Create()

// Create a subdirectory in the directory just created.
let dis = di.CreateSubdirectory "SubDir"

// Process that directory as required.
// ...

// Delete the subdirectory. The true indicates that if subdirectories
// or files are in this directory, they are to be deleted as well.
dis.Delete true

// Delete the directory.
di.Delete true
Imports System.IO

Public Class DeleteTest

    Public Shared Sub Main()
        ' Make a reference to a directory.
        Dim di As New DirectoryInfo("TempDir")

        ' Create the directory only if it does not already exist.
        If di.Exists = False Then
            di.Create()
        End If

        Dim dis As DirectoryInfo = di.CreateSubdirectory("SubDir")
        ' Create a subdirectory in the directory just created.

        ' Process that directory as required.
        ' ...

        ' Delete the subdirectory. The true indicates that if subdirectories
        ' or files are in this directory, they are to be deleted as well.
        dis.Delete(True)

        ' Delete the directory.
        di.Delete(True)
    End Sub
End Class

Remarks

If the DirectoryInfo has no files or subdirectories, this method deletes the DirectoryInfo even if recursive is false. Attempting to delete a DirectoryInfo that is not empty when recursive is false throws an IOException.

For a list of common I/O tasks, see Common I/O Tasks.

See also

Applies to