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

属性值

String

表示目录的完整路径的字符串。

例外

为目录名传入 null

完全限定路径名称的长度超出系统定义的最大长度。

调用方没有所要求的权限。

示例

以下示例检索指定文件的完整路径。

String^ fileName = "C:\\TMP\\log.txt";
FileInfo^ fileInfo = gcnew FileInfo( fileName );
if (  !fileInfo->Exists )
{
   return;
}

Console::WriteLine( " {0} has a directoryName of {1}",
   fileName, fileInfo->DirectoryName );
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
 */
Dim fileName As String = "C:\TMP\log.txt"
Dim fileInfo As New FileInfo(fileName)
If Not fileInfo.Exists Then
    Return
End If

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 以获取信息的最新副本。

适用于

另请参阅