Bagikan melalui


DirectoryInfo.Delete Metode

Definisi

Menghapus dan DirectoryInfo isinya dari jalur.

Overload

Delete()

Menghapus ini DirectoryInfo jika kosong.

Delete(Boolean)

Menghapus instans ini dari DirectoryInfo, menentukan apakah akan menghapus subdirektori dan file.

Delete()

Sumber:
DirectoryInfo.cs
Sumber:
DirectoryInfo.cs
Sumber:
DirectoryInfo.cs

Menghapus ini DirectoryInfo jika kosong.

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

Pengecualian

Direktori berisi file baca-saja.

Direktori yang dijelaskan oleh objek ini DirectoryInfo tidak ada atau tidak dapat ditemukan.

Direktori tidak kosong.

-atau-

Direktori adalah direktori kerja aplikasi saat ini.

-atau-

Ada handel terbuka pada direktori, dan sistem operasinya adalah Windows XP atau yang lebih lama. Handel terbuka ini dapat dihasilkan dari menghitung direktori. Untuk informasi selengkapnya, lihat Cara: Menghitung Direktori dan File.

Pemanggil tidak memiliki izin yang diperlukan.

Contoh

Contoh berikut memberikan pengecualian jika Anda mencoba menghapus direktori yang tidak kosong.

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

Keterangan

Untuk daftar tugas I/O umum, lihat Tugas I/O Umum.

Lihat juga

Berlaku untuk

Delete(Boolean)

Sumber:
DirectoryInfo.cs
Sumber:
DirectoryInfo.cs
Sumber:
DirectoryInfo.cs

Menghapus instans ini dari DirectoryInfo, menentukan apakah akan menghapus subdirektori dan file.

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 untuk menghapus direktori ini, subdirektorinya, dan semua file; jika tidak, false.

Pengecualian

Direktori berisi file baca-saja.

Direktori yang dijelaskan oleh objek ini DirectoryInfo tidak ada atau tidak dapat ditemukan.

Direktori bersifat baca-saja.

-atau-

Direktori berisi satu atau beberapa file atau subdirektori dan recursive adalah false.

-atau-

Direktori adalah direktori kerja aplikasi saat ini.

-atau-

Ada handel terbuka pada direktori atau pada salah satu filenya, dan sistem operasinya adalah Windows XP atau yang lebih lama. Handel terbuka ini dapat dihasilkan dari menghitung direktori dan file. Untuk informasi selengkapnya, lihat Cara: Menghitung Direktori dan File.

Pemanggil tidak memiliki izin yang diperlukan.

Contoh

Contoh berikut menunjukkan penghapusan direktori. Karena direktori dihapus, pertama-tama komentari Delete baris untuk menguji bahwa direktori ada. Kemudian batalkan komentar baris kode yang sama untuk menguji bahwa direktori berhasil dihapus.

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

Keterangan

DirectoryInfo Jika tidak memiliki file atau subdirektori, metode ini akan menghapus DirectoryInfo meskipun recursive adalah false. Mencoba menghapus yang DirectoryInfo tidak kosong saat recursive melempar falseIOException.

Untuk daftar tugas I/O umum, lihat Tugas I/O Umum.

Lihat juga

Berlaku untuk