次の方法で共有


DirectoryInfo.Delete メソッド

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

オーバーロードの一覧

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Overrides Public Sub Delete()

[C#] public override void Delete();

[C++] public: void Delete();

[JScript] public override function Delete();

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Sub Delete(Boolean)

[C#] public void Delete(bool);

[C++] public: void Delete(bool);

[JScript] public function Delete(Boolean);

使用例

ディレクトリを削除する例を次に示します。ディレクトリは削除されるため、まず 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();

参照

DirectoryInfo クラス | DirectoryInfo メンバ | System.IO 名前空間