FileInfo.DirectoryName 屬性

定義

取得表示目錄完整路徑的字串。

public:
 property System::String ^ DirectoryName { System::String ^ get(); };
public string DirectoryName { get; }
public string? DirectoryName { get; }
member this.DirectoryName : string
Public ReadOnly Property DirectoryName As String

屬性值

字串,表示目錄的完整路徑。

例外狀況

null 為此目錄名稱傳入。

完整路徑名稱超出系統定義的長度上限。

呼叫端沒有必要的權限。

範例

下列範例會擷取指定檔案的完整路徑。

string   fileName = @"C:\TMP\log.txt";
FileInfo fileInfo = new FileInfo(fileName);
if (!fileInfo.Exists)
{
    return;
}

Console.WriteLine("{0} has a directoryName of {1}",
    fileName, fileInfo.DirectoryName);
/* This code produces output similar to the following,
 * though actual results may vary by machine:
 *
 * C:\TMP\log.txt has a directory name of C:\TMP
 */

備註

若要取得父目錄做為 DirectoryInfo 物件,請使用 Directory 屬性。

第一次呼叫時, FileInfo 會呼叫 Refresh 和 快取檔案的相關信息。 在後續的呼叫中,您必須呼叫 Refresh 以取得資訊的最新複本。

適用於

另請參閱