In C#, Directory.GetParent() returns the folder that contains a given path. The result is a DirectoryInfo object, so you can read its Name, FullName, and Exists properties.
For a root folder such as C:\ or /, the answer is null because nothing is above it. The method only reads the path text, so the path does not have to exist.
This is a static method of System.IO.Directory. Call it as Directory.GetParent(...); there is no new Directory(). For an object that remembers one folder, see DirectoryInfo.
Syntax
Pass a path without a trailing separator. Always check the result for null when the path might be a root.
The same idea when you already have a DirectoryInfo object.
Path.GetDirectoryName gives the parent as a string, while Directory.GetParent gives a DirectoryInfo. Pick the type that fits your next step. Do not add a trailing separator to the path, because it changes what counts as the last part.
Library declarations
C#
public static DirectoryInfo? GetParent(string path);
Parameters
Parameter
Description
path
The file or folder path whose parent you want. It does not need to exist. It must not be empty.
Return Value
Return Value
Description
DirectoryInfo
The parent folder.
null
The path is a root, so there is no parent.
C# Examples
Example 1 — Read the Parent of a Nested Folder
Build two levels of folders, then read each parent name.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-parent-demo");
if (Directory.Exists(root)) Directory.Delete(root, true);
string level2 = Path.Combine(root, "level1", "level2");
Directory.CreateDirectory(level2);
DirectoryInfo up1 = Directory.GetParent(level2);
DirectoryInfo up2 = Directory.GetParent(up1.FullName);
Console.WriteLine(up1.Name);
Console.WriteLine(up2.Name);
Console.WriteLine(up1.Exists);
Directory.Delete(root, true);
}
}
Output
level1
codetofun-parent-demo
True
How It Works
Three levels. The folder level2 sits in level1, which sits in the demo root.
One step up.GetParent(level2) returns a DirectoryInfo for level1, and Name shows only that last part.
Another step up. The code passes up1.FullName back in, so the next parent is the demo root, codetofun-parent-demo.
It is a real object. Because the folder was created, up1.Exists is True. For a made-up path the same property would be False.
Example 2 — Climb Until There Is No Parent
Loop upward from a deep folder and count how many parents it has.
C#
using System;
using System.IO;
class Program {
static void Main() {
string deep = Path.Combine(Path.GetTempPath(), "codetofun-parent-a", "b", "c");
int steps = 0;
DirectoryInfo current = Directory.GetParent(deep);
while (current != null) {
steps++;
current = current.Parent;
}
Console.WriteLine("Climbed 3 or more levels: " + (steps >= 3));
Console.WriteLine("Ended at the top: " + (current == null));
}
}
Output
Climbed 3 or more levels: True
Ended at the top: True
How It Works
A path with no folders on disk. Nothing is created, because GetParent works on text.
First parent.GetParent(deep) gives the folder b. After that the loop uses the Parent property of DirectoryInfo.
Stop on null. The while loop ends when current becomes null, which only happens above the root.
No exact number printed. The temp folder is deeper on some computers. The path above it has at least three parents (b, codetofun-parent-a, and the temp folder), so the test steps >= 3 is True everywhere.
Example 3 — A Root Has No Parent and an Empty Path Throws
See null for a root and an ArgumentException for an empty path.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Directory.GetDirectoryRoot(Path.GetTempPath());
DirectoryInfo up = Directory.GetParent(root);
Console.WriteLine("Root parent is null: " + (up == null));
try {
Directory.GetParent("");
} catch (ArgumentException) {
Console.WriteLine("Empty path is not allowed");
}
string temp = Path.Combine(root, "tmp-demo");
Console.WriteLine(Directory.GetParent(temp).FullName == root);
}
}
Output
Root parent is null: True
Empty path is not allowed
True
How It Works
Find a root without printing it.GetDirectoryRoot turns the temp path into its root, and the program never shows the value.
Null at the top. There is no folder above a root, so GetParent(root) returns null instead of throwing.
Empty is a mistake. An empty string has no meaning as a path, so ArgumentException is thrown and caught.
One level below the root.tmp-demo sits directly under the root, so its parent is the root and the final comparison is True. The path is never created.
Frequently Asked Questions
A DirectoryInfo for the folder that contains the path. It returns null if the path is a root such as C:\ or /.
No. GetParent works with the text of the path, so a folder that is not on disk still has a parent.
GetParent returns a DirectoryInfo object. Path.GetDirectoryName returns a string. Both give the folder above a path.
Call GetParent twice, or use DirectoryInfo.Parent: Directory.GetParent(path)?.Parent. Check for null at each step.
The path is a root, which has no parent. Always check for null when your input could be a drive root.