FileInfo.DirectoryName Property

Definition

Gets a string representing the directory's full path.

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

Property Value

String

A string representing the directory's full path.

Exceptions

null 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.

Examples

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

Remarks

To get the parent directory as a DirectoryInfo object, use the Directory property.

When first called, FileInfo calls Refresh and caches information about the file. On subsequent calls, you must call Refresh to get the latest copy of the information.

Applies to

See also