DirectoryInfo.Delete メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
パスから DirectoryInfo とその内容を削除します。
オーバーロード
| 名前 | 説明 |
|---|---|
| Delete() |
この DirectoryInfo が空の場合は削除します。 |
| Delete(Boolean) |
サブディレクトリとファイルを削除するかどうかを指定して、 DirectoryInfoのこのインスタンスを削除します。 |
Delete()
この DirectoryInfo が空の場合は削除します。
public:
override void Delete();
public override void Delete();
override this.Delete : unit -> unit
Public Overrides Sub Delete ()
例外
ディレクトリには読み取り専用ファイルが含まれています。
この DirectoryInfo オブジェクトによって記述されたディレクトリが存在しないか、見つかりませんでした。
ディレクトリが空ではありません。
-または-
ディレクトリは、アプリケーションの現在の作業ディレクトリです。
-または-
ディレクトリに開いているハンドルがあり、オペレーティング システムがWindows XP以前です。 このオープン ハンドルは、ディレクトリを列挙した結果として発生する可能性があります。 詳細については、「 方法: ディレクトリとファイルを列挙する」を参照してください。
呼び出し元に必要なアクセス許可がありません。
例
次の例では、空でないディレクトリを削除しようとすると、例外がスローされます。
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)
サブディレクトリとファイルを削除するかどうかを指定して、 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 つ以上のファイルまたはサブディレクトリが含まれており、 recursive は false。
-または-
ディレクトリは、アプリケーションの現在の作業ディレクトリです。
-または-
ディレクトリまたはそのファイルのいずれかに開いているハンドルがあり、オペレーティング システムがWindows XP以前です。 このオープン ハンドルは、ディレクトリとファイルを列挙した結果として発生する可能性があります。 詳細については、「 方法: ディレクトリとファイルを列挙する」を参照してください。
呼び出し元に必要なアクセス許可がありません。
例
次の例では、ディレクトリの削除を示します。 ディレクトリが削除されるため、最初に Delete 行をコメント アウトして、ディレクトリが存在することをテストします。 次に、ディレクトリが正常に削除されたことをテストするために、同じコード行のコメントを解除します。
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
注釈
DirectoryInfoにファイルまたはサブディレクトリがない場合、このメソッドは、recursiveがfalseされている場合でも、DirectoryInfoを削除します。
recursiveがfalseIOExceptionをスローするときに空ではないDirectoryInfoを削除しようとしています。
一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。