Directory.Delete メソッド

定義

指定されたディレクトリとオプションでサブディレクトリを削除します。

オーバーロード

Delete(String)

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

Delete(String, Boolean)

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

Delete(String)

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

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 パラメーターの末尾から削除されます。

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

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

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

こちらもご覧ください

適用対象

Delete(String, Boolean)

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

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

path のディレクトリ、サブディレクトリ、およびファイルを削除する場合は true。それ以外の場合は 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メソッドで削除できない場合があります。

こちらもご覧ください

適用対象