DirectoryName Property
Collapse the table of content
Expand the table of content

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.

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

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show:
© 2016 Microsoft