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

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 = 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
 */

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