Share via


DirectoryInfo.Delete Yöntem

Tanım

Bir DirectoryInfo ve içeriğini bir yoldan siler.

Aşırı Yüklemeler

Delete()

Boşsa bunu DirectoryInfo siler.

Delete(Boolean)

Alt dizinlerin ve dosyaların silinip silinmeyeceğini belirten bir öğesinin DirectoryInfobu örneğini siler.

Delete()

Kaynak:
DirectoryInfo.cs
Kaynak:
DirectoryInfo.cs
Kaynak:
DirectoryInfo.cs

Boşsa bunu DirectoryInfo siler.

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

Özel durumlar

Dizin salt okunur bir dosya içerir.

Bu DirectoryInfo nesne tarafından açıklanan dizin yok veya bulunamadı.

Dizin boş değil.

-veya-

Dizin, uygulamanın geçerli çalışma dizinidir.

-veya-

Dizinde açık bir tanıtıcı vardır ve işletim sistemi Windows XP veya önceki bir sürümdür. Bu açık tanıtıcı dizinlerin numaralaştırılmasından kaynaklanabilir. Daha fazla bilgi için bkz . Nasıl yapılır: Dizinleri ve Dosyaları Listeleme.

Çağıranın gerekli izni yok.

Örnekler

Aşağıdaki örnek, boş olmayan bir dizini silmeye çalışırsanız bir özel durum oluşturur.

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

Açıklamalar

Yaygın G/Ç görevlerinin listesi için bkz. Ortak G/Ç Görevleri.

Ayrıca bkz.

Şunlara uygulanır

Delete(Boolean)

Kaynak:
DirectoryInfo.cs
Kaynak:
DirectoryInfo.cs
Kaynak:
DirectoryInfo.cs

Alt dizinlerin ve dosyaların silinip silinmeyeceğini belirten bir öğesinin DirectoryInfobu örneğini siler.

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

Parametreler

recursive
Boolean

true bu dizini, alt dizinlerini ve tüm dosyaları silmek için; aksi takdirde , false.

Özel durumlar

Dizin salt okunur bir dosya içerir.

Bu DirectoryInfo nesne tarafından açıklanan dizin yok veya bulunamadı.

Dizin salt okunurdur.

-veya-

Dizin bir veya daha fazla dosya veya alt dizin içerir ve recursive şeklindedir false.

-veya-

Dizin, uygulamanın geçerli çalışma dizinidir.

-veya-

Dizinde veya dosyalarından birinde açık bir tanıtıcı vardır ve işletim sistemi Windows XP veya önceki bir sürümdür. Bu açık tanıtıcı dizinlerin ve dosyaların numaralandırılıp listelenmesinden kaynaklanabilir. Daha fazla bilgi için bkz . Nasıl yapılır: Dizinleri ve Dosyaları Listeleme.

Çağıranın gerekli izni yok.

Örnekler

Aşağıdaki örnekte bir dizinin silinmesi gösterilmektedir. Dizin kaldırıldığından, dizinin mevcut olup olmadığını test etmek için önce Delete satırı açıklama satırı yapın. Ardından dizinin başarıyla kaldırıldığını test etmek için aynı kod satırının açıklamasını kaldırın.

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

Açıklamalar

DirectoryInfo dosyası veya alt dizini yoksa, bu yöntem olsa recursivefalsebile öğesini DirectoryInfo siler. bir olduğunda boş recursivefalse olmayan bir DirectoryInfo silme girişiminde bulunurIOException.

Yaygın G/Ç görevlerinin listesi için bkz. Ortak G/Ç Görevleri.

Ayrıca bkz.

Şunlara uygulanır