DirectoryInfo.Delete Metoda

Definicja

Usuwa DirectoryInfo i jego zawartość ze ścieżki.Deletes a DirectoryInfo and its contents from a path.

Przeciążenia

Delete()

Usuwa ten DirectoryInfo, jeśli jest pusty.Deletes this DirectoryInfo if it is empty.

Delete(Boolean)

Usuwa to wystąpienie DirectoryInfo, określając, czy należy usunąć podkatalogi i pliki.Deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.

Delete()

Usuwa ten DirectoryInfo, jeśli jest pusty.Deletes this DirectoryInfo if it is empty.

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

Wyjątki

Katalog zawiera plik tylko do odczytu.The directory contains a read-only file.

Katalog opisany przez ten obiekt DirectoryInfo nie istnieje lub nie można go znaleźć.The directory described by this DirectoryInfo object does not exist or could not be found.

Katalog nie jest pusty.The directory is not empty.

lub-or- Katalog jest bieżącym katalogiem roboczym aplikacji.The directory is the application's current working directory.

lub-or- Istnieje otwarte dojście do katalogu, a system operacyjny to Windows XP lub wcześniejsza wersja.There is an open handle on the directory, and the operating system is Windows XP or earlier. To otwarte dojście może wynikać z wyliczania katalogów.This open handle can result from enumerating directories. Aby uzyskać więcej informacji, zobacz jak: Wyliczanie katalogów i plików.For more information, see How to: Enumerate Directories and Files.

Element wywołujący nie ma wymaganego uprawnienia.The caller does not have the required permission.

Przykłady

Poniższy przykład zgłasza wyjątek, Jeśli podjęto próbę usunięcia katalogu, który nie jest pusty.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 {}
    }
}
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

Uwagi

Aby uzyskać listę typowych zadań we/wy, zobacz typowe zadania we/wy.For a list of common I/O tasks, see Common I/O Tasks.

Zobacz też

Delete(Boolean)

Usuwa to wystąpienie DirectoryInfo, określając, czy należy usunąć podkatalogi i pliki.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)

Parametry

recursive
Boolean

true usunąć tego katalogu, jego podkatalogów i wszystkie pliki; w przeciwnym razie false.true to delete this directory, its subdirectories, and all files; otherwise, false.

Wyjątki

Katalog zawiera plik tylko do odczytu.The directory contains a read-only file.

Katalog opisany przez ten obiekt DirectoryInfo nie istnieje lub nie można go znaleźć.The directory described by this DirectoryInfo object does not exist or could not be found.

Katalog jest tylko do odczytu.The directory is read-only.

lub-or- Katalog zawiera jeden lub więcej plików lub podkatalogów, a recursive jest false.The directory contains one or more files or subdirectories and recursive is false.

lub-or- Katalog jest bieżącym katalogiem roboczym aplikacji.The directory is the application's current working directory.

lub-or- Istnieje otwarte dojście do katalogu lub jednego z jego plików, a system operacyjny to Windows XP lub wcześniejsza wersja.There is an open handle on the directory or on one of its files, and the operating system is Windows XP or earlier. To otwarte dojście może wynikać z wyliczania katalogów i plików.This open handle can result from enumerating directories and files. Aby uzyskać więcej informacji, zobacz jak: Wyliczanie katalogów i plików.For more information, see How to: Enumerate Directories and Files.

Element wywołujący nie ma wymaganego uprawnienia.The caller does not have the required permission.

Przykłady

Poniższy przykład demonstruje usunięcie katalogu.The following example demonstrates deleting a directory. Ponieważ katalog jest usuwany, najpierw Skomentuj wiersz Delete, aby sprawdzić, czy katalog istnieje.Because the directory is removed, first comment out the Delete line to test that the directory exists. Następnie Usuń komentarz z tego samego wiersza kodu, aby sprawdzić, czy katalog został usunięty pomyślnie.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);
    }
}
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

Uwagi

Jeśli DirectoryInfo nie ma żadnych plików ani podkatalogów, ta metoda usuwa DirectoryInfo nawet wtedy, gdy recursive jest false.If the DirectoryInfo has no files or subdirectories, this method deletes the DirectoryInfo even if recursive is false. Podjęto próbę usunięcia DirectoryInfo, która nie jest pusta, gdy recursive jest false zgłasza IOException.Attempting to delete a DirectoryInfo that is not empty when recursive is false throws an IOException.

Aby uzyskać listę typowych zadań we/wy, zobacz typowe zadania we/wy.For a list of common I/O tasks, see Common I/O Tasks.

Zobacz też

Dotyczy