Directory.Delete メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定されたディレクトリとオプションでサブディレクトリを削除します。
オーバーロード
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
によって指定されたディレクトリは空ではありません。または - ディレクトリが読み取り専用になっているか、または読み取り専用のファイルが含まれています。
または - ディレクトリは別のプロセスによって使用されています。
呼び出し元に、必要なアクセス許可がありません。
.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path
長さ 0 の文字列、空白のみを含む、または無効な文字が 1 つ以上含まれています。 正しくない文字を照会するには、GetInvalidPathChars() メソッドを使用します。
path
が null
です。
指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。
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
、大文字と小文字は区別されません。
場合によっては、指定したディレクトリをエクスプローラーで開いている場合、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
で指定されたディレクトリが読み取り専用です。または、recursive
がfalse
であり、path
は空のディレクトリではありません。または - ディレクトリはアプリケーションの現在の作業ディレクトリです。
または - ディレクトリに読み取り専用のファイルが含まれています。
または - ディレクトリは別のプロセスによって使用されています。
呼び出し元に、必要なアクセス許可がありません。
.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path
長さ 0 の文字列、空白のみを含む、または無効な文字が 1 つ以上含まれています。 正しくない文字を照会するには、GetInvalidPathChars() メソッドを使用します。
path
が null
です。
指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。
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
、大文字と小文字は区別されません。
パラメーターが指定されている recursive
場合、ユーザーは true
現在のディレクトリとすべてのサブディレクトリに対する書き込みアクセス許可を持っている必要があります。
シンボリック リンクやマウント ポイントなど、再解析ポイントを含むディレクトリを削除する場合、このメソッドの動作は若干異なります。 再解析ポイントがマウント ポイントなどのディレクトリである場合は、マウント解除され、マウント ポイントが削除されます。 このメソッドは、再解析ポイントを繰り返しません。 再解析ポイントがファイルへのシンボリック リンクである場合、再解析ポイントは削除され、シンボリック リンクのターゲットは削除されません。
場合によっては、指定したディレクトリをエクスプローラーで開いている場合、Deleteメソッドで削除できない場合があります。