GetParent Method

Directory.GetParent Method (String)


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

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

public static DirectoryInfo GetParent(
	string path


Type: System.String

The path for which to retrieve the parent directory.

Return Value

Type: System.IO.DirectoryInfo

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

Exception Condition

The directory specified by path is read-only.


The caller does not have the required permission.


path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters with the GetInvalidPathChars method.


path is null.


The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.


The specified path was not found.

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 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 DirectorySeparatorChar or AltDirectorySeparatorChar. For example, passing the path "C:\Directory\SubDirectory\test.txt" to GetParent 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.

The following example demonstrates how to use the GetParent method to retrieve the parent directory of a user-specified location, "path". The value returned by the GetParent method is then printed to the console. The example is configured to catch all errors common to this method.

Snippet section 4 of code snippet {"project_id":"3fedad16-eaf1-41a6-8f96-0c1949c68f32","entity_id":"caa9851d-1f4b-4024-9302-cca596bd8277","entity_type":"CodeSnippet","locale":"en-US"} in source file ({"filename":"/CS/class1.cs","blob_type":"Source","blob_id":"-002fcs-002fclass1-002ecs","blob_revision":2}) overlaps with other snippet sections. Ensure the tags are placed correctly.

for reading from files or directories. Associated enumeration: FileIOPermissionAccess.Read

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Return to top
© 2015 Microsoft