Export (0) Print
Expand All

FileInfo.DirectoryName Property

Gets a string representing the directory's full path.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

public string DirectoryName { get; }

Property Value

Type: System.String
A string representing the directory's full path.

ExceptionCondition
ArgumentNullException

null was passed in for the directory name.

PathTooLongException

The fully qualified path is 260 or more characters.

SecurityException

The caller does not have the required permission.

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.

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

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft