In C#, Directory.GetDirectoryRoot() returns the root of a path: the very top folder such as C:\ on Windows, or / on Linux and macOS. It is the folder that has no parent.
The method only works with text. The path does not have to exist, and it can be relative, in which case the working directory is used to find the root.
This is a static method of System.IO.Directory. Call it as Directory.GetDirectoryRoot(...); there is no new Directory(). For an object that remembers one folder, see DirectoryInfo.
Syntax
Pass any path. The answer is the same for every folder on the same drive.
C#
string path = Path.Combine(Path.GetTempPath(), "reports", "2026");
string root = Directory.GetDirectoryRoot(path);
Console.WriteLine(Path.IsPathRooted(root)); // True
// root itself is "C:\" or "/" and varies by machine
Call
What it does
Directory.GetDirectoryRoot(path)
The root of path, after it is made absolute.
Path.GetPathRoot(path)
A very similar method of the Path class. It does not make a relative path absolute first.
Directory.GetParent(root)
Is null for a root, because a root has no parent.
The real value is machine-dependent: C:\, D:\, \\server\share\, or /. These examples compare it with other results or check its type, instead of printing it.
Library declarations
C#
public static string GetDirectoryRoot(string path);
Parameters
Parameter
Description
path
A file or folder path. It may be missing from the disk. It must not be null or empty.
Return Value
Return Value
Description
string
The root of the full path, for example C:\ or /.
C# Examples
Example 1 — Compare With Path.GetPathRoot
Both methods should agree for a full path, and the root should exist.
C#
using System;
using System.IO;
class Program {
static void Main() {
string path = Path.Combine(Path.GetTempPath(), "codetofun-root-demo", "inner");
string a = Directory.GetDirectoryRoot(path);
string b = Path.GetPathRoot(Path.GetFullPath(path));
Console.WriteLine(string.Equals(a, b, StringComparison.OrdinalIgnoreCase));
Console.WriteLine(Directory.Exists(a));
Console.WriteLine(Path.IsPathRooted(a));
}
}
Output
True
True
True
How It Works
Never printed directly. The root is C:\ on one computer and / on another, so the program prints comparisons instead.
Two methods, one answer. For an already full path, Directory.GetDirectoryRoot and Path.GetPathRoot return the same text, so Equals prints True.
Ignore case on purpose.OrdinalIgnoreCase avoids a false False if a drive letter differs in case.
A root is a real folder.Directory.Exists(a) is True, and a root is always rooted, so the last line is True. The demo folder in the path was never created.
Example 2 — Get the Root of a Folder That Does Not Exist
Show that the call uses only the text of the path, and that two deep paths share one root.
C#
using System;
using System.IO;
class Program {
static void Main() {
string one = Path.Combine(Path.GetTempPath(), "does-not-exist", "x", "y");
string two = Path.Combine(Path.GetTempPath(), "other-folder");
Console.WriteLine("Folder exists: " + Directory.Exists(one));
string rootOne = Directory.GetDirectoryRoot(one);
string rootTwo = Directory.GetDirectoryRoot(two);
Console.WriteLine("Root found: " + (rootOne.Length > 0));
Console.WriteLine("Same root: " + (rootOne == rootTwo));
}
}
Output
Folder exists: False
Root found: True
Same root: True
How It Works
A made-up path.does-not-exist\x\y sits under the temp folder but was never created, so Directory.Exists is False.
The root is still known. The method reads only the start of the text, so it finds a root without the folder being real.
Not empty.rootOne.Length > 0 is True because a full path always has a root.
Same drive, same root. Both paths begin in the temp folder, so the roots are equal. This is a quick way to test whether two paths are on one drive.
Example 3 — Relative Paths and the Top of the Tree
A relative name uses the working directory. A root has no parent folder.
C#
using System;
using System.IO;
class Program {
static void Main() {
string cwd = Directory.GetCurrentDirectory();
string fromRelative = Directory.GetDirectoryRoot("reports");
string fromCwd = Directory.GetDirectoryRoot(cwd);
Console.WriteLine("Relative uses cwd: " + (fromRelative == fromCwd));
string again = Directory.GetDirectoryRoot(fromCwd);
Console.WriteLine("Root of a root is itself: " + (again == fromCwd));
DirectoryInfo parent = Directory.GetParent(fromCwd);
Console.WriteLine("Root has no parent: " + (parent == null));
}
}
Output
Relative uses cwd: True
Root of a root is itself: True
Root has no parent: True
How It Works
Relative names borrow the working directory."reports" has no drive, so .NET first makes it full using the current folder. Its root therefore matches the root of cwd.
Repeating changes nothing. Asking for the root of a root returns the same text, so the second line is True.
Top of the tree.Directory.GetParent returns null for a root, because there is no folder above it.
Safe on every system. All three lines compare values with each other, so they are True on Windows, Linux, and macOS.
Frequently Asked Questions
The root of the given path, such as C:\ on Windows or / on Linux and macOS.
No. The method only reads the text of the path, so it works for folders that do not exist.
They are very close. Directory.GetDirectoryRoot first makes the path absolute, so a relative path gives the root of the working directory. Path.GetPathRoot returns an empty string for a relative name.
For a path like \\server\share\folder, the root is \\server\share.
It is different on every machine. Compare it with another value, or check Path.IsPathRooted(root), so the output does not depend on the reader's computer.