DirectoryInfo(String) コンストラクター

定義

指定したパスの DirectoryInfo クラスの新しいインスタンスを初期化します。

public:
 DirectoryInfo(System::String ^ path);
public DirectoryInfo(string path);
new System.IO.DirectoryInfo : string -> System.IO.DirectoryInfo
Public Sub New (path As String)

パラメーター

path
String

DirectoryInfoを作成するパスを指定する文字列。

例外

pathnullです。

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

.NET Framework および .NET Core バージョン 2.1 より前のバージョン: path には、"、<、>、|などの無効な文字が含まれています。

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

次の例では、このコンストラクターを使用して指定したディレクトリとサブディレクトリを作成し、サブディレクトリを含むディレクトリを削除できないことを示します。

using System;
using System.IO;

class Test
{
    public static void Main()
    {
        // Specify the directories you want to manipulate.
        DirectoryInfo di1 = new DirectoryInfo(@"c:\MyDir");
        DirectoryInfo di2 = new DirectoryInfo(@"c:\MyDir\temp");

        try
        {
            // Create the directories.
            di1.Create();
            di2.Create();

            // This operation will not be allowed because there are subdirectories.
            Console.WriteLine("I am about to attempt to delete {0}.", di1.Name);
            di1.Delete();
            Console.WriteLine("The Delete operation was successful, which was unexpected.");
        }
        catch (Exception)
        {
            Console.WriteLine("The Delete operation failed as expected.");
        }
        finally {}
    }
}
open System.IO

// Specify the directories you want to manipulate.
let di1 = DirectoryInfo @"c:\MyDir"
let di2 = DirectoryInfo @"c:\MyDir\temp"

try
    // Create the directories.
    di1.Create()
    di2.Create()

    // This operation will not be allowed because there are subdirectories.
    printfn $"I am about to attempt to delete {di1.Name}."
    di1.Delete()
    printfn "The Delete operation was successful, which was unexpected."
with _ ->
    printfn "The Delete operation failed as expected."
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim di1 As DirectoryInfo = New DirectoryInfo("c:\MyDir")
        Dim di2 As DirectoryInfo = New DirectoryInfo("c:\MyDir\temp")
        Try
            ' Create the directories.
            di1.Create()
            di2.Create()
            ' This operation will not be allowed because there are subdirectories.
            Console.WriteLine("I am about to attempt to delete {0}.", di1.Name)
            di1.Delete()
            Console.WriteLine("The Delete operation was successful, which was unexpected.")
        Catch e As Exception
            Console.WriteLine("The Delete operation failed as expected.")
        End Try
    End Sub
End Class

注釈

このコンストラクターは、ディレクトリが存在するかどうかを確認しません。 このコンストラクターは、後続の操作でディスクにアクセスするために使用される文字列のプレースホルダーです。

path パラメーターには、汎用名前付け規則 (UNC) 共有上のファイルを含むファイル名を指定できます。

Caution

特定のカルチャ設定で文字のセットをコンパイルし、異なるカルチャ設定で同じ文字を取得すると、文字が解釈できず、例外がスローされる可能性があります。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

適用対象

こちらもご覧ください