DirectoryInfo.Delete メソッド ()
DirectoryInfo が空の場合に、そのインスタンスを削除します。
Overrides Overloads Public Sub Delete()
[C#]
public override void Delete();
[C++]
public: void Delete();
[JScript]
public override function Delete();
例外
例外の種類 | 条件 |
---|---|
IOException | ディレクトリが空ではありません。 |
SecurityException | 呼び出し元に、必要なアクセス許可がありません。 |
解説
このメソッドの使用例については、以下の「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。
実行するタスク | 参考例があるトピック |
---|---|
ディレクトリをコピーする。 | Directory |
ディレクトリの名前を変更、またはディレクトリを移動する。 | Directory.Move |
ファイルを削除する。 | File.Delete |
ディレクトリを作成する。 | CreateDirectory |
サブディレクトリを作成する。 | CreateSubdirectory |
ディレクトリ内のファイルを参照する。 | Name |
ディレクトリ内のサブディレクトリを参照する。 | GetDirectories |
ディレクトリ内のすべてのサブディレクトリにあるすべてのファイルを参照する。 | GetFileSystemInfos |
使用例
[Visual Basic, C#, C++] 空でないディレクトリを削除しようとした場合に例外をスローする例を次に示します。
Imports System
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
[C#]
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 {}
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
int main() {
// Specify the directories you want to manipulate.
DirectoryInfo* di1 = new DirectoryInfo(S"c:\\MyDir");
try {
// Create the directories.
di1->Create();
di1->CreateSubdirectory(S"temp");
//This operation will not be allowed because there are subdirectories.
Console::WriteLine(S"I am about to attempt to delete {0}", di1->Name);
di1->Delete();
Console::WriteLine(S"The Delete operation was successful, which was unexpected.");
} catch (Exception*) {
Console::WriteLine(S"The Delete operation failed as expected.");
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: 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 セキュリティ:
- FileIOPermission (ディレクトリに書き込むために必要な許可) FileIOPermissionAccess.Write (関連する列挙体)
参照
DirectoryInfo クラス | DirectoryInfo メンバ | System.IO 名前空間 | DirectoryInfo.Delete オーバーロードの一覧 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み