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 element DirectoryInfo i jego zawartość ze ścieżki.
Przeciążenia
| Nazwa | Opis |
|---|---|
| Delete() |
Usuwa to DirectoryInfo , jeśli jest on pusty. |
| 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
- Źródło:
- DirectoryInfo.cs
- Źródło:
- DirectoryInfo.cs
Usuwa to DirectoryInfo , jeśli jest on pusty.
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 —
Istnieje otwarte dojście do katalogu, a system operacyjny jest Windows XP lub starszy. Ten otwarty uchwyt może wynikać z wyliczania katalogów. Aby uzyskać więcej informacji, zobacz Instrukcje: wyliczanie katalogów i plików.
Obiekt wywołujący nie ma wymaganych uprawnień.
Przykłady
Poniższy przykład zgłasza wyjątek, jeśli próbujesz usunąć katalog, który nie jest pusty.
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ż
- Wejście/Wyjście plików i strumieni
- 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
- Ź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 —
Istnieje otwarte dojście do katalogu lub w jednym z jego plików, a system operacyjny jest Windows XP lub starszy. Ten otwarty uchwyt może wynikać z wyliczania katalogów i plików. Aby uzyskać więcej informacji, zobacz Instrukcje: wyliczanie katalogów i plików.
Obiekt wywołujący nie ma wymaganych uprawnień.
Przykłady
W poniższym przykładzie pokazano usuwanie katalogu. Ponieważ katalog jest usuwany, najpierw oznacz wiersz jako Delete komentarz, aby sprawdzić, czy katalog istnieje. Następnie usuń komentarz z tego samego wiersza kodu, aby sprawdzić, czy katalog został pomyślnie usunięty.
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)
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 DirectoryInfo , ta metoda usuwa element nawet wtedy, gdy recursive ma wartość 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ż
- Wejście/Wyjście plików i strumieni
- Instrukcje: odczytywanie tekstu z pliku
- Instrukcje: zapisywanie tekstu w pliku