System.IO.Directory.GetParent Method

Retrieves the parent directory of the specified path, including both absolute and relative paths.

Syntax

public static DirectoryInfo GetParent (string path)

Parameters

path
The path for which to retrieve the parent directory.

Returns

The parent directory, or null if path is the root directory, including the root of a UNC server or share name.

Remarks

The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.

Trailing spaces are removed from the end of the path parameter before getting the directory.

The string returned by this method consists of all characters in the path up to, but not including, the last Path.DirectorySeparatorChar or Path.AltDirectorySeparatorChar. For example, passing the path "C:\Directory\SubDirectory\test.txt" to Directory.GetParent(string) returns "C:\Directory\SubDirectory". Passing "C:\Directory\SubDirectory" returns "C:\Directory". However, passing "C:\Directory\SubDirectory\" returns "C:\Directory\SubDirectory", because the ending directory separator is after "SubDirectory".

The path parameter is not case-sensitive.

For a list of common I/O tasks, see Common I/O Tasks.

Requirements

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0