次の方法で共有


Directory.Delete メソッド

ディレクトリとその内容を削除します。

オーバーロードの一覧

指定したパスから空のディレクトリを削除します。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Sub Delete(String)

[C#] public static void Delete(string);

[C++] public: static void Delete(String*);

[JScript] public static function Delete(String);

指定したディレクトリと、特に指定されている場合はディレクトリ内の任意のサブディレクトリを削除します。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Sub Delete(String, Boolean)

[C#] public static void Delete(string, bool);

[C++] public: static void Delete(String*, bool);

[JScript] public static function Delete(String, Boolean);

使用例

[Visual Basic, C#, C++] 指定したディレクトリおよびサブディレクトリを作成および削除する例を次に示します。

[Visual Basic, C#, C++] メモ   ここでは、Delete のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim path As String = "c:\MyDir"
        Dim subPath As String = "c:\MyDir\temp"

        Try
            ' Determine whether the directory exists.
            If Directory.Exists(path) = False Then
                ' Create the directory.
                Directory.CreateDirectory(path)
            End If

            ' Determine whether the directory exists.
            If Directory.Exists(subPath) = False Then
                ' Create the directory.
                Directory.CreateDirectory(subPath)
            End If

            'This operation will not be allowed because there are subdirectories.
            Console.WriteLine("I am about to attempt to delete {0}", path)
            Directory.Delete(path, True)
            Console.WriteLine("The Delete operation was successful.")

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        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.
        string path = @"c:\MyDir";
        string subPath = @"c:\MyDir\temp";

        try 
        {
            // Determine whether the directory exists.
            if (!Directory.Exists(path)) 
            {
                // Create the directory.
                Directory.CreateDirectory(path);
            }


            if (!Directory.Exists(subPath)) 
            {
                // Create the directory.
                Directory.CreateDirectory(subPath);
            }

            // This will succeed because subdirectories are being deleted.
            Console.WriteLine("I am about to attempt to delete {0}", path);
            Directory.Delete(path, true);
            Console.WriteLine("The Delete operation was successful.");

        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        } 
        finally {}
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    // Specify the directories you want to manipulate.
    String* path = S"c:\\MyDir";
    String* subPath = S"c:\\MyDir\\temp";

    try {
        // Determine whether the directory exists.
        if (!Directory::Exists(path)) {
            // Create the directory.
            Directory::CreateDirectory(path);
        }

        if (!Directory::Exists(subPath)) {
            // Create the directory.
            Directory::CreateDirectory(subPath);
        }

        // This will succeed because subdirectories are being deleted.
        Console::WriteLine(S"I am about to attempt to delete {0}", path);
        Directory::Delete(path, true);
        Console::WriteLine(S"The Delete operation was successful.");
    } catch (Exception* e) {
        Console::WriteLine(S"The process failed: {0}", e);
    } 
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

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