DirectoryInfo.Delete メソッド (Boolean)
中に含まれているサブディレクトリとファイルを削除するかどうかを指定して、 DirectoryInfo のインスタンスを削除します。
Overloads Public Sub Delete( _
ByVal recursive As Boolean _)
[C#]
public void Delete(boolrecursive);
[C++]
public: void Delete(boolrecursive);
[JScript]
public function Delete(
recursive : Boolean);
パラメータ
- recursive
このディレクトリ、そのサブディレクトリ、およびすべてのファイルを削除する場合は true 。それ以外の場合は false 。
例外
例外の種類 | 条件 |
---|---|
IOException | ディレクトリが読み取り専用です。
または ディレクトリに 1 つ以上のファイルまたはサブディレクトリが含まれており、 recursive が false です。 |
SecurityException | 呼び出し元に、必要なアクセス許可がありません。 |
解説
DirectoryInfo にファイルまたはサブディレクトリが含まれていない場合、このメソッドは、 recursive が false であっても DirectoryInfo を削除します。 recursive が false の場合に、空ではない DirectoryInfo を削除しようとすると、 IOException がスローされます。
このメソッドの使用例については、以下の「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。
実行するタスク | 参考例があるトピック |
---|---|
ディレクトリをコピーする。 | Directory |
ディレクトリの名前を変更、またはディレクトリを移動する。 | Directory.Move |
ファイルを削除する。 | File.Delete |
ディレクトリを作成する。 | CreateDirectory |
サブディレクトリを作成する。 | CreateSubdirectory |
ディレクトリ内のファイルを参照する。 | Name |
ディレクトリ内のサブディレクトリを参照する。 | GetDirectories |
ディレクトリ内のすべてのサブディレクトリにあるすべてのファイルを参照する。 | GetFileSystemInfos |
使用例
ディレクトリを削除する例を次に示します。ディレクトリは削除されるため、まず Delete 行をコメント化して、ディレクトリが存在することをテストしてください。次に、この行のコメントを外して、ディレクトリが正常に削除されることをテストします。
Imports System
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 'Main
End Class 'DeleteTest
[C#]
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);
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
int main() {
// Make a reference to a directory.
DirectoryInfo* di = new DirectoryInfo(S"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(S"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);
}
[JScript]
import System;
import System.IO;
public class DeleteTest {
public static function Main() {
// Make a reference to a directory.
var di : DirectoryInfo = 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.
var dis : DirectoryInfo = 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);
}
}
DeleteTest.Main();
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
.NET Framework セキュリティ:
- FileIOPermissionAccess Write フラグ (削除対象の DirectoryInfo とサブディレクトリに書き込むために必要なアクセス許可)
参照
DirectoryInfo クラス | DirectoryInfo メンバ | System.IO 名前空間 | DirectoryInfo.Delete オーバーロードの一覧 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み