DirectoryInfo.Delete Metoda

Definice

Odstraní DirectoryInfo jeho obsah z cesty.

Přetížení

Delete()

Odstraní ho DirectoryInfo , pokud je prázdný.

Delete(Boolean)

Odstraní tuto instanci objektu DirectoryInfoa určuje, zda se mají odstranit podadresáře a soubory.

Delete()

Zdroj:
DirectoryInfo.cs
Zdroj:
DirectoryInfo.cs
Zdroj:
DirectoryInfo.cs

Odstraní ho DirectoryInfo , pokud je prázdný.

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

Výjimky

Adresář obsahuje soubor jen pro čtení.

Adresář popsaný tímto DirectoryInfo objektem neexistuje nebo nebyl nalezen.

Adresář není prázdný.

-nebo-

Adresář je aktuální pracovní adresář aplikace.

-nebo-

V adresáři je otevřený popisovač a operační systém je Windows XP nebo starší. Tento otevřený popisovač může být výsledkem výčtu adresářů. Další informace najdete v tématu Postupy: Výčet adresářů a souborů.

Volající nemá požadované oprávnění.

Příklady

Následující příklad vyvolá výjimku, pokud se pokusíte odstranit adresář, který není prázdný.

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

Poznámky

Seznam běžných vstupně-výstupních úloh najdete v tématu Běžné vstupně-výstupní úlohy.

Viz také

Platí pro

Delete(Boolean)

Zdroj:
DirectoryInfo.cs
Zdroj:
DirectoryInfo.cs
Zdroj:
DirectoryInfo.cs

Odstraní tuto instanci objektu DirectoryInfoa určuje, zda se mají odstranit podadresáře a soubory.

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

Parametry

recursive
Boolean

trueodstranit tento adresář, jeho podadresáře a všechny soubory; v opačném případě . false

Výjimky

Adresář obsahuje soubor jen pro čtení.

Adresář popsaný tímto DirectoryInfo objektem neexistuje nebo nebyl nalezen.

Adresář je jen pro čtení.

-nebo-

Adresář obsahuje jeden nebo více souborů nebo podadresářů a recursive je false.

-nebo-

Adresář je aktuální pracovní adresář aplikace.

-nebo-

V adresáři nebo v jednom z jeho souborů je otevřený popisovač a operační systém je Windows XP nebo starší. Výsledkem tohoto otevřeného popisovače může být výčet adresářů a souborů. Další informace najdete v tématu Postupy: Výčet adresářů a souborů.

Volající nemá požadované oprávnění.

Příklady

Následující příklad ukazuje odstranění adresáře. Vzhledem k tomu, že je adresář odebrán, nejprve zakomentujte Delete řádek a otestujte, že adresář existuje. Potom odkomentujte stejný řádek kódu a otestujte, že se adresář úspěšně odebral.

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

Poznámky

Pokud neobsahuje DirectoryInfo žádné soubory nebo podadresáře, tato metoda odstraní i když DirectoryInforecursive je false. Při pokusu o odstranění objektuDirectoryInfo, který není prázdný, vyvolá recursivefalse se .IOException

Seznam běžných vstupně-výstupních úloh najdete v tématu Běžné vstupně-výstupní úlohy.

Viz také

Platí pro