DirectoryInfo(String) DirectoryInfo(String) DirectoryInfo(String) DirectoryInfo(String) Constructor

定義

指定したパスに対する、DirectoryInfo クラスの新しいインスタンスを初期化します。Initializes a new instance of the DirectoryInfo class on the specified path.

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 String String String

DirectoryInfo を作成する対象のパスを指定する文字列。A string specifying the path on which to create the DirectoryInfo.

例外

呼び出し元に、必要なアクセス許可がありません。The caller does not have the required permission.

path に、"、<、>、| など、無効な文字が含まれています。path contains invalid characters such as ", <, >, or |.

指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。The specified path, file name, or both exceed the system-defined maximum length.

次の例では、このコンス トラクターを使用して、指定されたディレクトリとサブディレクトリを作成し、サブディレクトリを格納するディレクトリを削除できないことを示します。The following example uses this constructor to create the specified directory and subdirectory, and demonstrates that a directory that contains subdirectories cannot be deleted.

using namespace System;
using namespace System::IO;
int main()
{
   
   // Specify the directories you want to manipulate.
   DirectoryInfo^ di1 = gcnew DirectoryInfo( "c:\\MyDir" );
   DirectoryInfo^ di2 = gcnew 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." );
   }

}

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 {}
    }
}
Imports System
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

注釈

このコンス トラクターでは、ディレクトリが存在するかどうかはチェックされません。This constructor does not check if a directory exists. このコンス トラクターでは、以降の操作でディスクへのアクセスに使用される文字列のプレース ホルダーです。This constructor is a placeholder for a string that is used to access the disk in subsequent operations.

pathパラメーターは、汎用名前付け規則 (UNC) 共有上のファイルを含む、ファイル名を指定できます。The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.

注意事項

特定のカルチャ設定と一連の文字をコンパイルして、同じ文字を別のカルチャ設定を取得すると、文字は、解釈されないことしがスローされる例外が発生する可能性があります。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。For a list of common I/O tasks, see Common I/O Tasks.

セキュリティ

FileIOPermission
ファイルとディレクトリから読み取る。for reading from files and directories. 関連付けられた列挙体。 ReadAssociated enumeration: Read

適用対象

こちらもご覧ください