FileInfo.DirectoryName 属性

定义

获取表示目录的完整路径的字符串。Gets a string representing the directory's full path.

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

属性值

表示目录的完整路径的字符串。A string representing the directory's full path.

异常

为目录名传入 nullnull was passed in for the directory name.

完全限定路径名称的长度超出系统定义的最大长度。The fully qualified path name exceeds the system-defined maximum length.

调用方没有所要求的权限。The caller does not have the required permission.

示例

下面的示例检索指定文件的完整路径。The following example retrieves the full path of the specified file.

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使用属性。To get the parent directory as a DirectoryInfo object, use the Directory property.

第一次调用FileInfoRefresh , 将调用并缓存有关文件的信息。When first called, FileInfo calls Refresh and caches information about the file. 在后续调用中, 必须调用Refresh以获取信息的最新副本。On subsequent calls, you must call Refresh to get the latest copy of the information.

安全性

FileIOPermission
用于读取文件。for reading files. 关联的枚举:FileIOPermissionAccessAssociated enumeration: FileIOPermissionAccess

适用于

另请参阅