DirectoryInfo(String) 建構函式

定義

在指定的路徑上,初始化 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

字串,指定用來建立 DirectoryInfo 的路徑。A string specifying the path on which to create the DirectoryInfo.

例外狀況

pathnullpath is null.

呼叫端沒有必要的權限。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.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.

適用於

另請參閱