DirectoryInfo.Delete Methode

Definition

Löscht ein DirectoryInfo und dessen Inhalt aus einem Pfad.

Überlädt

Delete()

Löscht diese DirectoryInfo, wenn diese leer sind.

Delete(Boolean)

Löscht diese Instanz von DirectoryInfo und gibt an, ob Unterverzeichnisse und Dateien gelöscht werden sollen.

Delete()

Quelle:
DirectoryInfo.cs
Quelle:
DirectoryInfo.cs
Quelle:
DirectoryInfo.cs

Löscht diese DirectoryInfo, wenn diese leer sind.

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

Ausnahmen

Das Verzeichnis enthält eine schreibgeschützte Datei.

Das von diesem DirectoryInfo-Objekt beschriebene Verzeichnis ist nicht vorhanden, oder es konnte nicht gefunden werden.

Das Verzeichnis ist nicht leer.

- oder -

Das Verzeichnis ist das aktuelle Arbeitsverzeichnis der Anwendung.

- oder -

Für das Verzeichnis ist ein geöffnetes Handle vorhanden, und das Betriebssystem ist Windows XP oder früher. Dieses geöffnete Handle kann vom Auflisten von Verzeichnissen stammen. Weitere Informationen finden Sie unter Gewusst wie: Auflisten von Verzeichnissen und Dateien.

Der Aufrufer verfügt nicht über die erforderliche Berechtigung.

Beispiele

Im folgenden Beispiel wird eine Ausnahme ausgelöst, wenn Sie versuchen, ein Verzeichnis zu löschen, das nicht leer ist.

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

Hinweise

Eine Liste der allgemeinen E/A-Aufgaben finden Sie unter Allgemeine E/A-Aufgaben.

Weitere Informationen

Gilt für:

Delete(Boolean)

Quelle:
DirectoryInfo.cs
Quelle:
DirectoryInfo.cs
Quelle:
DirectoryInfo.cs

Löscht diese Instanz von DirectoryInfo und gibt an, ob Unterverzeichnisse und Dateien gelöscht werden sollen.

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

Parameter

recursive
Boolean

true, um dieses Verzeichnis, seine Unterverzeichnisse und alle Dateien zu löschen, andernfalls false.

Ausnahmen

Das Verzeichnis enthält eine schreibgeschützte Datei.

Das von diesem DirectoryInfo-Objekt beschriebene Verzeichnis ist nicht vorhanden, oder es konnte nicht gefunden werden.

Das Verzeichnis ist schreibgeschützt.

- oder -

Das Verzeichnis enthält mindestens eine Datei oder ein Unterverzeichnis, und recursive ist false.

- oder -

Das Verzeichnis ist das aktuelle Arbeitsverzeichnis der Anwendung.

- oder -

Für das Verzeichnis oder eine der Dateien darin ist ein geöffnetes Handle vorhanden, und das Betriebssystem ist Windows XP oder früher. Dieses geöffnete Handle kann vom Auflisten von Verzeichnissen und Dateien stammen. Weitere Informationen finden Sie unter Gewusst wie: Auflisten von Verzeichnissen und Dateien.

Der Aufrufer verfügt nicht über die erforderliche Berechtigung.

Beispiele

Im folgenden Beispiel wird das Löschen eines Verzeichnisses veranschaulicht. Da das Verzeichnis entfernt wurde, kommentieren Sie zunächst die Delete Zeile aus, um zu testen, dass das Verzeichnis vorhanden ist. Heben Sie dann die Auskommentierung derselben Codezeile auf, um zu testen, dass das Verzeichnis erfolgreich entfernt wurde.

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

Hinweise

Wenn keine DirectoryInfo Dateien oder Unterverzeichnisse vorhanden sind, löscht diese Methode auch dann, DirectoryInfo wenn recursive ist false. Der Versuch, eine DirectoryInfo zu löschen, die nicht leer ist, wenn recursive ausgelöst wird falseIOException.

Eine Liste der allgemeinen E/A-Aufgaben finden Sie unter Allgemeine E/A-Aufgaben.

Weitere Informationen

Gilt für: