DirectoryInfo.Delete メソッド

定義

パスから DirectoryInfo とその内容を削除します。

オーバーロード

Delete()

DirectoryInfo が空の場合に、そのインスタンスを削除します。

Delete(Boolean)

中に含まれているサブディレクトリとファイルを削除するかどうかを指定して、DirectoryInfo のインスタンスを削除します。

Delete()

Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs

DirectoryInfo が空の場合に、そのインスタンスを削除します。

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

例外

ディレクトリに読み取り専用のファイルが含まれています。

この DirectoryInfo オブジェクトに記述されているディレクトリが存在しないか、見つかりませんでした。

ディレクトリが空ではありません。

- または -

ディレクトリはアプリケーションの現在の作業ディレクトリです。

- または -

開いているハンドルがディレクトリに対して存在し、オペレーティング システムが Windows XP またはそれ以前のものです。 このハンドルが開いている原因は、ディレクトリを列挙したことにある可能性があります。 詳細については、「 方法: ディレクトリとファイルを列挙する」をご覧ください。

呼び出し元に、必要なアクセス許可がありません。

次の例では、空ではないディレクトリを削除しようとすると、例外がスローされます。

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

注釈

共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。

こちらもご覧ください

適用対象

Delete(Boolean)

Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs

中に含まれているサブディレクトリとファイルを削除するかどうかを指定して、DirectoryInfo のインスタンスを削除します。

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

パラメーター

recursive
Boolean

このディレクトリ、そのサブディレクトリ、およびすべてのファイルを削除する場合は true。それ以外の場合は false

例外

ディレクトリに読み取り専用のファイルが含まれています。

この DirectoryInfo オブジェクトに記述されているディレクトリが存在しないか、見つかりませんでした。

ディレクトリが読み取り専用です。

- または -

ディレクトリに 1 つ以上のファイルまたはサブディレクトリが含まれており、recursivefalse です。

- または -

ディレクトリはアプリケーションの現在の作業ディレクトリです。

- または -

開いているハンドルがディレクトリまたはそのディレクトリ内のファイルに対して存在し、オペレーティング システムが Windows XP またはそれ以前のものです。 このハンドルが開いている原因は、ディレクトリおよびファイルを列挙したことにある可能性があります。 詳細については、「 方法: ディレクトリとファイルを列挙する」をご覧ください。

呼び出し元に、必要なアクセス許可がありません。

次の例では、ディレクトリを削除する方法を示します。 ディレクトリが削除されるため、最初に行を Delete コメントアウトして、ディレクトリが存在することをテストします。 次に、ディレクトリが正常に削除されたことをテストするために、同じコード行のコメントを解除します。

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

注釈

DirectoryInfoファイルまたはサブディレクトリがない場合、このメソッドは が falseの場合recursiveでも をDirectoryInfo削除します。 が をスローIOExceptionしたときにrecursivefalse空ではない をDirectoryInfo削除しようとしています。

共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。

こちらもご覧ください

適用対象