語言

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路徑。

例外狀況

path 是 null。

來電者沒有所需的權限。

.NET Framework 及 2.1 .NET Core 版本: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)共享上的檔案。

注意事項

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

關於常見 I/O 任務的清單,請參見 Common I/O 任務。

適用於

另請參閱