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和 2.1 之前的 .NET Core 版本: path 包含無效字元,例如 「、、 <> 或 |。

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

範例

下列範例會使用此建構函式來建立指定的目錄和子目錄,並示範無法刪除包含子目錄的目錄。

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 {}
    }
}
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) 共用。

警告

當您使用特定文化設定編譯一組字元,並使用不同的文化設定擷取相同的字元時,字元可能無法解譯,而且可能會導致擲回例外狀況。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

適用於

另請參閱