DirectoryInfo.Delete Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Usuwa obiekt DirectoryInfo i jego zawartość ze ścieżki.
Przeciążenia
| Delete() |
Usuwa tę wartość DirectoryInfo , jeśli jest pusta. |
| Delete(Boolean) |
Usuwa to wystąpienie DirectoryInfoobiektu , określając, czy usunąć podkatalogi i pliki. |
Delete()
- Źródło:
- DirectoryInfo.cs
- Źródło:
- DirectoryInfo.cs
- Źródło:
- DirectoryInfo.cs
Usuwa tę wartość DirectoryInfo , jeśli jest pusta.
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.
Katalog opisany przez ten DirectoryInfo obiekt nie istnieje lub nie można go odnaleźć.
Katalog nie jest pusty.
-lub-
Katalog jest bieżącym katalogem roboczym aplikacji.
-lub-
W katalogu znajduje się otwarte dojście, a system operacyjny to Windows XP lub starszy. Ten otwarty uchwyt może wynikać z wyliczania katalogów. Aby uzyskać więcej informacji, zobacz How to: Enumerate Directoryies and Files (Instrukcje: wyliczanie katalogów i plików).
Obiekt wywołujący nie posiada wymaganych uprawnień.
Przykłady
Poniższy przykład zgłasza wyjątek, jeśli próbujesz usunąć katalog, który nie jest pusty.
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
Uwagi
Aby uzyskać listę typowych zadań we/wy, zobacz Typowe zadania we/wy.
Zobacz też
- We/wy plików i Stream
- Instrukcje: Odczytywanie tekstu z pliku
- Instrukcje: Zapisywanie tekstu w pliku
Dotyczy
Delete(Boolean)
- Źródło:
- DirectoryInfo.cs
- Źródło:
- DirectoryInfo.cs
- Źródło:
- DirectoryInfo.cs
Usuwa to wystąpienie DirectoryInfoobiektu , określając, czy usunąć podkatalogi i pliki.
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 aby usunąć ten katalog, jego podkatalogi i wszystkie pliki; w przeciwnym razie , false.
Wyjątki
Katalog zawiera plik tylko do odczytu.
Katalog opisany przez ten DirectoryInfo obiekt nie istnieje lub nie można go odnaleźć.
Katalog jest tylko do odczytu.
-lub-
Katalog zawiera jeden lub więcej plików lub podkatalogów i recursive jest .false
-lub-
Katalog jest bieżącym katalogem roboczym aplikacji.
-lub-
W katalogu lub w jednym z jego plików istnieje otwarte dojście, a system operacyjny to Windows XP lub starszy. Ten otwarty uchwyt może wynikać z wyliczania katalogów i plików. Aby uzyskać więcej informacji, zobacz How to: Enumerate Directoryies and Files (Instrukcje: wyliczanie katalogów i plików).
Obiekt wywołujący nie posiada wymaganych uprawnień.
Przykłady
W poniższym przykładzie pokazano usuwanie katalogu. Ponieważ katalog jest usuwany, najpierw oznacz wiersz jako komentarz, Delete aby sprawdzić, czy katalog istnieje. Następnie usuń znaczniki komentarza z tego samego wiersza kodu, aby sprawdzić, czy katalog został pomyślnie usunięty.
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
Uwagi
Jeśli obiekt DirectoryInfo nie ma plików ani podkatalogów, ta metoda usuwa metodę DirectoryInfo nawet wtedy, gdy recursive jest to false. Próba usunięcia elementu DirectoryInfo , który nie jest pusty, gdy recursive jest false zgłaszany element IOException.
Aby uzyskać listę typowych zadań we/wy, zobacz Typowe zadania we/wy.
Zobacz też
- We/wy plików i Stream
- Instrukcje: Odczytywanie tekstu z pliku
- Instrukcje: Zapisywanie tekstu w pliku