In C#, Path.IsPathRooted() returns true when a path starts from a root: a leading separator (/data/x.txt), or on Windows a drive (C:\x, C:x). A relative path such as data/x.txt or ../x.txt is not rooted.
The method reads text only and never checks the disk. null and empty strings give false. Note that "rooted" is a weaker test than "fully qualified": on Windows "/data/x.txt" and "C:x" are rooted but still depend on the current drive or folder.
This is a static method of System.IO.Path. Call it as Path.IsPathRooted(...); 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. The result is a bool.
C#
bool a = Path.IsPathRooted("/data/x.txt"); // true (on every system)
bool b = Path.IsPathRooted("data/x.txt"); // false
bool c = Path.IsPathRooted("../x.txt"); // false
Call
What it does
IsPathRooted("/data/x.txt")
true on every system.
IsPathRooted("data/x.txt")
false: relative.
IsPathRooted("C:/data/x.txt")
Windows: true. Linux and macOS: false.
IsPathRooted("C:x")
Windows: true (drive-relative, not fully qualified). Linux and macOS: false.
IsPathRooted(null)
false. No exception.
If you need to know whether the path is complete on its own, use Path.IsPathFullyQualified instead. A rooted path can still depend on the working drive.
Library declarations
C#
public static bool IsPathRooted(string? path);
public static bool IsPathRooted(ReadOnlySpan<char> path);
Parameters
Parameter
Description
path
The path to test. It may be null or empty and it does not need to exist.
Return Value
Return Value
Description
bool
true if the path begins with a directory separator or, on Windows, a drive such as C:. Otherwise false, including for null and empty.
C# Examples
Example 1 — Test Rooted and Relative Paths
These inputs give the same answers on every system.
C#
using System;
using System.IO;
class Program {
static void Main() {
string[] paths = { "/data/x.txt", "data/x.txt", "../x.txt", "./x.txt", "" };
foreach (string p in paths) {
Console.WriteLine("[" + p + "] " + Path.IsPathRooted(p));
}
}
}
A leading separator./data/x.txt starts with /, so it is rooted. On Windows a slash at the start also counts, so this is True there too.
Plain relative names.data/x.txt starts with a name, so it is relative and the answer is False.
Dots are not roots.../x.txt and ./x.txt start from the working folder or its parent, so both are False.
Empty is not rooted. The last item is an empty string. The square brackets in the output make it visible, and the answer is False with no exception.
Example 2 — Use IsPathRooted to Guard Path.Combine
Reject a rooted file name before it can override the upload folder, and see what this check does not catch.
C#
using System;
using System.IO;
class Program {
static string SafeJoin(string baseFolder, string name) {
if (Path.IsPathRooted(name)) {
return "rejected";
}
return Path.Combine(baseFolder, name);
}
static void Main() {
Console.WriteLine(SafeJoin("uploads", "photo.png").Replace('\\', '/'));
Console.WriteLine(SafeJoin("uploads", "/etc/passwd"));
Console.WriteLine(SafeJoin("uploads", "../secret.txt").Replace('\\', '/'));
}
}
Output
uploads/photo.png
rejected
uploads/../secret.txt
How It Works
The guard.SafeJoin refuses any name that is rooted. A rooted name would replace the base folder inside Combine.
A normal name passes.photo.png is relative, so it is joined to uploads. Replace makes the separator the same on every system.
A rooted name is stopped./etc/passwd is rooted, so "rejected" is returned and Combine never runs.
A gap remains.../secret.txt is relative, so the guard lets it through, and the result still contains ... A full check should resolve the path with Path.GetFullPath and test where it ends up.
Example 3 — null, Empty, and Full Paths
The method is forgiving with missing input, and every full path from the system is rooted.
C#
using System;
using System.IO;
class Program {
static void Main() {
string missing = null;
Console.WriteLine("null: " + Path.IsPathRooted(missing));
Console.WriteLine("empty: " + Path.IsPathRooted(string.Empty));
Console.WriteLine("dot: " + Path.IsPathRooted("."));
Console.WriteLine("temp path: " + Path.IsPathRooted(Path.GetTempPath()));
Console.WriteLine("full path: " + Path.IsPathRooted(Path.GetFullPath("notes.txt")));
Console.WriteLine("file name only: " + Path.IsPathRooted(Path.GetFileName(Path.Combine(Path.GetTempPath(), "a.txt"))));
}
}
Output
null: False
empty: False
dot: False
temp path: True
full path: True
file name only: False
How It Works
No exception.null and the empty string both give False, so you can test an optional setting without a separate null check.
A dot is relative. The single dot means "the working folder", which is a relative start, so the answer is False.
System paths are rooted.GetTempPath and GetFullPath both return complete paths that begin at a root, so the answers are True on every system.
Cutting off the folder part.GetFileName keeps only a.txt, which has no root, so the last line is False. This is the usual way to turn a rooted path into a safe name.
Frequently Asked Questions
It returns true when the path starts with a root: a separator such as "/" or, on Windows, a drive such as "C:". Relative paths, null, and empty strings give false.
Rooted means "starts from a root". Fully qualified means "points to one exact place, whatever the current folder or drive". On Windows, "/data/x" and "C:x" are rooted but not fully qualified. On Linux the two tests agree.
Because Combine drops earlier pieces when a later one is rooted. Combine("uploads", "/etc/passwd") is "/etc/passwd". Rejecting rooted input first keeps the result inside your folder.
No. It only looks at the characters at the start. A rooted path to a missing folder still gives true. Use Path.Exists for a disk check.
No. null and the empty string both return false. That makes it safe to call on input that may be missing.