Directory.Delete メソッド

定義

指定したディレクトリと、必要に応じて任意のサブディレクトリを削除します。

オーバーロード

名前 説明
Delete(String)

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

Delete(String, Boolean)

指定したディレクトリを削除し、指定されている場合はディレクトリ内のサブディレクトリとファイルを削除します。

Delete(String)

ソース:
Directory.cs
ソース:
Directory.cs
ソース:
Directory.cs
ソース:
Directory.cs
ソース:
Directory.cs

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

public:
 static void Delete(System::String ^ path);
public static void Delete(string path);
static member Delete : string -> unit
Public Shared Sub Delete (path As String)

パラメーター

path
String

削除する空のディレクトリの名前。 このディレクトリは書き込み可能で空である必要があります。

例外

pathで指定された同じ名前と場所を持つファイルが存在します。

-又は-

ディレクトリは、アプリケーションの現在の作業ディレクトリです。

-又は-

pathで指定されたディレクトリが空ではありません。

-又は-

ディレクトリが読み取り専用であるか、読み取り専用ファイルが含まれています。

-又は-

ディレクトリは別のプロセスで使用されています。

呼び出し元に必要なアクセス許可がありません。

2.1 より前のバージョンの .NET Framework と .NET Core: path は長さ 0 の文字列で、空白のみを含むか、1 つ以上の無効な文字を含みます。 GetInvalidPathChars() メソッドを使用して、無効な文字のクエリを実行できます。

pathnullです。

指定したパス、ファイル名、またはその両方が、システム定義の最大長を超えています。

path が存在しないか、見つかりませんでした。

-又は-

指定されたパスが無効です (たとえば、マップされていないドライブ上にあります)。

次の例は、新しいディレクトリとサブディレクトリを作成し、サブディレクトリのみを削除する方法を示しています。

using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string subPath = @"C:\NewDirectory\NewSubDirectory";

            try
            {
                Directory.CreateDirectory(subPath);
                Directory.Delete(subPath);

                bool directoryExists = Directory.Exists(@"C:\NewDirectory");
                bool subDirectoryExists = Directory.Exists(subPath);

                Console.WriteLine("top-level directory exists: " + directoryExists);
                Console.WriteLine("sub-directory exists: " + subDirectoryExists);
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.Message);
            }
        }
    }
}
open System.IO

let subPath = @"C:\NewDirectory\NewSubDirectory"

try
    Directory.CreateDirectory subPath |> ignore
    Directory.Delete subPath

    let directoryExists = Directory.Exists @"C:\NewDirectory"
    let subDirectoryExists = Directory.Exists subPath

    printfn $"top-level directory exists: {directoryExists}"
    printfn $"sub-directory exists: {subDirectoryExists}"
with e ->
    printfn $"The process failed: {e.Message}"
Imports System.IO

Module Module1

    Sub Main()
        Dim subPath = "C:\NewDirectory\NewSubDirectory"

        Try
            Directory.CreateDirectory(subPath)
            Directory.Delete(subPath)

            Dim directoryExists = Directory.Exists("C:\NewDirectory")
            Dim subDirectoryExists = Directory.Exists(subPath)

            Console.WriteLine("top-level directory exists: " & directoryExists)
            Console.WriteLine("sub-directory exists: " & subDirectoryExists)

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.Message)
        End Try
    End Sub

End Module

注釈

このメソッドは、2 番目のパラメーターにDelete(String, Boolean)指定されたfalseと同じように動作します。

pathパラメーターは、相対パス情報または絶対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。 現在の作業ディレクトリを取得するには、 GetCurrentDirectoryを参照してください。

末尾のスペースは、ディレクトリを削除する前に、 path パラメーターの末尾から削除されます。

このメソッドは、IOException パラメーターで指定されたディレクトリにファイルまたはサブディレクトリが含まれている場合、pathをスローします。

path パラメーターの大文字と小文字の区別は、コードが実行されているファイル システムの大文字と小文字が区別されます。 たとえば、NTFS (既定の Windows ファイル システム) では大文字と小文字が区別され、Linux ファイル システムでは大文字と小文字が区別されます。

場合によっては、指定したディレクトリをエクスプローラーで開いている場合、 Delete メソッドで削除できない場合があります。

こちらもご覧ください

適用対象

Delete(String, Boolean)

ソース:
Directory.cs
ソース:
Directory.cs
ソース:
Directory.cs
ソース:
Directory.cs
ソース:
Directory.cs

指定したディレクトリを削除し、指定されている場合はディレクトリ内のサブディレクトリとファイルを削除します。

public:
 static void Delete(System::String ^ path, bool recursive);
public static void Delete(string path, bool recursive);
static member Delete : string * bool -> unit
Public Shared Sub Delete (path As String, recursive As Boolean)

パラメーター

path
String

削除するディレクトリの名前。

recursive
Boolean

true path内のディレクトリ、サブディレクトリ、およびファイルを削除する場合は。それ以外の場合はfalse

例外

pathで指定された同じ名前と場所を持つファイルが存在します。

-又は-

pathで指定されたディレクトリが読み取り専用であるか、recursivefalseされ、pathが空のディレクトリではありません。

-又は-

ディレクトリは、アプリケーションの現在の作業ディレクトリです。

-又は-

ディレクトリには読み取り専用ファイルが含まれています。

-又は-

ディレクトリは別のプロセスで使用されています。

呼び出し元に必要なアクセス許可がありません。

2.1 より前のバージョンの .NET Framework と .NET Core: path は長さ 0 の文字列で、空白のみを含むか、1 つ以上の無効な文字を含みます。 GetInvalidPathChars() メソッドを使用して、無効な文字のクエリを実行できます。

pathnullです。

指定したパス、ファイル名、またはその両方が、システム定義の最大長を超えています。

path が存在しないか、見つかりませんでした。

-又は-

指定されたパスが無効です (たとえば、マップされていないドライブ上にあります)。

次の例は、サブディレクトリに新しいディレクトリ、サブディレクトリ、ファイルを作成し、新しい項目をすべて再帰的に削除する方法を示しています。

using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string topPath = @"C:\NewDirectory";
            string subPath = @"C:\NewDirectory\NewSubDirectory";

            try
            {
                Directory.CreateDirectory(subPath);

                using (StreamWriter writer = File.CreateText(subPath + @"\example.txt"))
                {
                    writer.WriteLine("content added");
                }

                Directory.Delete(topPath, true);

                bool directoryExists = Directory.Exists(topPath);

                Console.WriteLine("top-level directory exists: " + directoryExists);
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.Message);
            }
        }
    }
}
open System.IO

let topPath = @"C:\NewDirectory"
let subPath = @"C:\NewDirectory\NewSubDirectory"

try
    Directory.CreateDirectory(subPath) |> ignore

    do
        use writer = File.CreateText(subPath + @"\example.txt")
        writer.WriteLine "content added"

    Directory.Delete(topPath, true)

    let directoryExists = Directory.Exists topPath

    printfn $"top-level directory exists: {directoryExists}"
    
with e ->
    printfn $"The process failed: {e.Message}"
Imports System.IO

Module Module1

    Sub Main()
        Dim topPath = "C:\NewDirectory"
        Dim subPath = "C:\NewDirectory\NewSubDirectory"

        Try
            Directory.CreateDirectory(subPath)

            Using writer As StreamWriter = File.CreateText(subPath + "\example.txt")
                writer.WriteLine("content added")
            End Using

            Directory.Delete(topPath, True)

            Dim directoryExists = Directory.Exists(topPath)

            Console.WriteLine("top-level directory exists: " & directoryExists)
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.Message)
        End Try
    End Sub

End Module

注釈

pathパラメーターは、相対パス情報または絶対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。 現在の作業ディレクトリを取得するには、 GetCurrentDirectoryを参照してください。

末尾のスペースは、ディレクトリを削除する前に、 path パラメーターの末尾から削除されます。

path パラメーターの大文字と小文字の区別は、コードが実行されているファイル システムの大文字と小文字が区別されます。 たとえば、NTFS (既定の Windows ファイル システム) では大文字と小文字が区別され、Linux ファイル システムでは大文字と小文字が区別されます。

recursive パラメーターがtrue場合、ユーザーは現在のディレクトリとすべてのサブディレクトリに対する書き込みアクセス許可を持っている必要があります。

シンボリック リンクやマウント ポイントなど、再解析ポイントを含むディレクトリを削除する場合、このメソッドの動作は若干異なります。 再解析ポイントがマウント ポイントなどのディレクトリである場合は、マウント解除され、マウント ポイントが削除されます。 このメソッドは、再解析ポイントを繰り返しません。 再解析ポイントがファイルへのシンボリック リンクである場合、再解析ポイントは削除され、シンボリック リンクのターゲットは削除されません。

場合によっては、指定したディレクトリをエクスプローラーで開いている場合、 Delete メソッドで削除できない場合があります。

こちらもご覧ください

適用対象