C# Path GetPathRoot() Method

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

Definition

In C#, Path.GetPathRoot() returns the root of a path: the part that says where the path starts. On Linux and macOS the root is /. On Windows it is a drive such as C:\, a share such as \\server\share, or a lone \.

A relative path has no root, so the answer is an empty string. A null path gives null. The method reads text only. It does not check that the drive exists. Because roots are system-specific, some inputs give different answers on different systems, and the examples label that clearly.

This is a static method of System.IO.Path. Call it as Path.GetPathRoot(...); there is no new Path(). Like most Path methods, it only works with text: it never opens, reads, or creates a file, and it works the same whether or not the path exists.

Syntax

Pass a path and read the root part.

C#
string root = Path.GetPathRoot("/data/reports/2026.txt");

Console.WriteLine(root.Replace('\\', '/'));   // /  (on every system)
Console.WriteLine(Path.GetPathRoot("data/a.txt").Length);   // 0 (relative)
CallWhat it does
GetPathRoot("/data/a.txt")Returns / (a lone \ on Windows).
GetPathRoot("data/a.txt")Relative. Returns an empty string.
GetPathRoot("C:/data/a.txt")Windows: C:\. Linux and macOS: an empty string, because this is just a relative name there.
GetPathRoot("/")The path is the root. Returns /.
GetPathRoot(null)Returns null.

If you want the root of the full version of a relative path, call Path.GetFullPath first. For a real folder, Directory.GetDirectoryRoot does both steps.

Library declarations
C#
public static string? GetPathRoot(string? path);
public static ReadOnlySpan<char> GetPathRoot(ReadOnlySpan<char> path);

Parameters

ParameterDescription
pathThe path to read. It may be absolute or relative, and it does not need to exist.

Return Value

Return ValueDescription
string?The root part, or an empty string if the path has no root, or null if path is null.

C# Examples

Example 1 — Read the Root of Absolute and Relative Paths

These inputs behave the same on every system once the result goes through Replace.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        Console.WriteLine(Path.GetPathRoot("/data/reports/2026.txt").Replace('\\', '/'));
        Console.WriteLine("[" + Path.GetPathRoot("reports/2026.txt") + "]");
        Console.WriteLine(Path.GetPathRoot("/").Replace('\\', '/'));

        string missing = null;
        Console.WriteLine(Path.GetPathRoot(missing) == null);
    }
}

How It Works

  1. An absolute path. A path that starts with a separator has a root made of that separator. Windows may answer with a backslash, so Replace turns it into / for printing.
  2. A relative path. reports/2026.txt does not say where it starts, so the root is an empty string. The brackets show that clearly.
  3. The root itself. When the whole path is just /, the root is that same text.
  4. null in, null out. A null path gives null, with no exception, so the comparison is True.

Example 2 — A Drive Path: Different Answers on Different Systems

The path C:/data/a.txt is a drive path on Windows only. The output below shows both outcomes honestly.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.GetPathRoot("C:/data/a.txt");

        Console.WriteLine("[" + root.Replace('\\', '/') + "]");
        Console.WriteLine("Has a root: " + (root.Length > 0));
        Console.WriteLine("Rooted: " + Path.IsPathRooted("C:/data/a.txt"));
    }
}

How It Works

  1. Run it once, get one of two outputs. Only the block that matches your system appears when you run the program; both are shown here because the answer depends on the operating system.
  2. On Windows. A letter, a colon and a slash make a drive root. The root is C:\, and Replace shows it as C:/ between the brackets.
  3. On Linux and macOS. A path only starts at the root when it begins with /. This one begins with a letter, so it is relative: the root is empty, and IsPathRooted is False.
  4. The takeaway. Do not assume that drive-style text means something on every system. A program that must run everywhere should build its paths with Combine and GetTempPath instead of fixed drive letters.

Example 3 — The Root of the Full Path and the Temp Path

Machine-independent facts about the root of real paths: the working directory and the temp folder.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string temp = Path.GetTempPath();
        string tempRoot = Path.GetPathRoot(temp);

        Console.WriteLine("Temp has a root: " + (tempRoot.Length > 0));
        Console.WriteLine("Temp starts with its root: " + temp.StartsWith(tempRoot, StringComparison.Ordinal));

        string full = Path.GetFullPath("notes.txt");
        string cwdRoot = Path.GetPathRoot(Directory.GetCurrentDirectory());
        Console.WriteLine("Same root as the working folder: " + (Path.GetPathRoot(full) == cwdRoot));
        Console.WriteLine("Relative name has no root: " + (Path.GetPathRoot("notes.txt").Length == 0));
    }
}

How It Works

  1. The temp path is absolute. GetTempPath always gives a full path, so its root is not empty on any system. The drive letter or / itself is never printed.
  2. The root is the start. A root is the first part of its path, so the temp path starts with it and StartsWith is True.
  3. A full path keeps the root. GetFullPath("notes.txt") puts the working directory in front of the name. Its root is therefore the root of the working directory.
  4. A bare name has none. notes.txt is relative, so its root has length zero. That is the same test as !Path.IsPathRooted for ordinary names.

Frequently Asked Questions

The root of the path: "/" on Linux and macOS, and on Windows a drive such as "C:\", a network share, or a lone backslash. For a relative path it returns an empty string.
Call Path.GetPathRoot on a full Windows path, then take the first character. On Linux and macOS there is no drive letter, so check Path.DirectorySeparatorChar first or use the root as it is.
The path is relative, so it has no root. Call Path.GetFullPath first, or use Directory.GetDirectoryRoot, which makes the path full before it reads the root.
No. The root is the very top: "/" or "C:\". The parent is the folder one level up, which GetDirectoryName gives. They are the same only for a path directly under the root, such as "/data".
Because each system has its own rules. "C:/data/a.txt" starts with a drive on Windows, but on Linux it is just a relative name with a colon in it. That is normal, and the examples label both outcomes.

Next: GetRandomFileName()

Get a random, cryptographically strong 8.3 name such as abcdefgh.xyz. No file is created.

GetRandomFileName() 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.

8 people found this page helpful