次の方法で共有


Directory.Delete メソッド (String)

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

Overloads Public Shared Sub Delete( _
   ByVal path As String _)
[C#]
public static void Delete(stringpath);
[C++]
public: static void Delete(String* path);
[JScript]
public static function Delete(
   path : String);

パラメータ

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

例外

例外の種類 条件
IOException path によって指定されたディレクトリが、読み取り専用であるか、空ではありません。
UnauthorizedAccessException 呼び出し元に、必要なアクセス許可がありません。
ArgumentException path が、長さが 0 の文字列であるか、空白しか含んでいないか、または InvalidPathChars で定義されている無効な文字を 1 つ以上含んでいます。
ArgumentNullException path が null 参照 (Visual Basic では Nothing) です。
PathTooLongException 指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。たとえば、Windows ベースのプラットフォームの場合、パスの長さは 248 文字未満、ファイル名の長さは 260 文字未満である必要があります。
DirectoryNotFoundException 割り当てられていないドライブであるなど、指定されたパスが無効です。

解説

このメソッドは、 false を指定した Delete(String, Boolean) とまったく同じ動作をします。

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

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

path パラメータでは大文字と小文字が区別されません。

このメソッドの使用例については、以下の「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。

実行するタスク 参考例があるトピック
ディレクトリをコピーする。 Directory
ディレクトリの名前を変更、またはディレクトリを移動する。 Directory.Move

DirectoryInfo.MoveTo

ファイルを削除する。 File.Delete

FileInfo.Delete

ディレクトリを作成する。 CreateDirectory

Directory

サブディレクトリを作成する。 CreateSubdirectory
ディレクトリ内のファイルを参照する。 Name
ディレクトリ内のサブディレクトリを参照する。 GetDirectories

GetDirectories

ディレクトリ内のすべてのサブディレクトリにあるすべてのファイルを参照する。 GetFileSystemInfos

.NET Compact Framework - Windows CE .NET プラットフォームに関する注意点: 一部のデバイスのファイル システムは相対パスをサポートしていません。絶対パス情報を指定してください。

使用例

[Visual Basic, C#, C++] 指定したディレクトリを削除し、サブディレクトリが存在する場合に例外をスローする例を次に示します。

 
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 if it does not exist.
                Directory.CreateDirectory(path)
            End If

            ' Determine whether the subdirectory exists.
            If Directory.Exists(subPath) = False Then
                ' Create the subdirectory if it does not exist.
                Directory.CreateDirectory(SubPath)
            End If

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

        Catch
            Console.WriteLine("The Delete operation failed as expected.")
        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 if it does not exist.
                Directory.CreateDirectory(path);
            }


            if (!Directory.Exists(subPath)) 
            {
                // Create the subdirectory if it does not exist.
                Directory.CreateDirectory(subPath);
            }

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

        } 
        catch (Exception) 
        {
            Console.WriteLine("The Delete operation failed as expected.");
        } 
        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 if it does not exist.
            Directory::CreateDirectory(path);
        }

        if (!Directory::Exists(subPath)) {
            // Create the subdirectory if it does not exist.
            Directory::CreateDirectory(subPath);
        }

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

    } catch (Exception*) {
        Console::WriteLine(S"The Delete operation failed as expected.");
    } 
}

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

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

.NET Framework セキュリティ:

参照

Directory クラス | Directory メンバ | System.IO 名前空間 | Directory.Delete オーバーロードの一覧 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み