C# Directory GetParent() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

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.

C#
string folder = Path.Combine(Path.GetTempPath(), "reports", "2026");

DirectoryInfo parent = Directory.GetParent(folder);

if (parent != null) {
    Console.WriteLine(parent.Name);   // reports
}
CallWhat it does
Directory.GetParent(path)The folder that holds path, as a DirectoryInfo.
Directory.GetParent(root)null, because a root has no parent.
new DirectoryInfo(path).ParentThe 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

ParameterDescription
pathThe file or folder path whose parent you want. It does not need to exist. It must not be empty.

Return Value

Return ValueDescription
DirectoryInfoThe parent folder.
nullThe 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);
    }
}

How It Works

  1. Three levels. The folder level2 sits in level1, which sits in the demo root.
  2. One step up. GetParent(level2) returns a DirectoryInfo for level1, and Name shows only that last part.
  3. Another step up. The code passes up1.FullName back in, so the next parent is the demo root, codetofun-parent-demo.
  4. 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));
    }
}

How It Works

  1. A path with no folders on disk. Nothing is created, because GetParent works on text.
  2. First parent. GetParent(deep) gives the folder b. After that the loop uses the Parent property of DirectoryInfo.
  3. Stop on null. The while loop ends when current becomes null, which only happens above the root.
  4. 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);
    }
}

How It Works

  1. Find a root without printing it. GetDirectoryRoot turns the temp path into its root, and the program never shows the value.
  2. Null at the top. There is no folder above a root, so GetParent(root) returns null instead of throwing.
  3. Empty is a mistake. An empty string has no meaning as a path, so ArgumentException is thrown and caught.
  4. 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.

Next: Move()

Move or rename a folder together with everything inside it.

Move() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful