Edit

Share via


Path.IsPathRooted Method

Definition

Returns a value that indicates whether a file path contains a root.

Overloads

IsPathRooted(String)

Returns a value indicating whether the specified path string contains a root.

IsPathRooted(ReadOnlySpan<Char>)

Returns a value that indicates whether the specified character span that represents a file path contains a root.

Remarks

A rooted path is file path that is fixed to a specific drive or UNC path; it contrasts with a path that is relative to the current drive or working directory. For example, on Windows systems, a rooted path begins with a backslash (for example, "\Documents") or a drive letter and colon (for example, "C:Documents").

Note that rooted paths can be either absolute (that is, fully qualified) or relative. An absolute rooted path is a fully qualified path from the root of a drive to a specific directory. A relative rooted path specifies a drive, but its fully qualified path is resolved against the current directory. The following example illustrates the difference.

C#
using System;
using System.IO;

class Program
{
    static void Main()
    {
        string relative1 = "C:Documents"; 
        ShowPathInfo(relative1);

        string relative2 = "/Documents";
        ShowPathInfo(relative2);

        string absolute = "C:/Documents";
        ShowPathInfo(absolute);
    }

    private static void ShowPathInfo(string path)
    {
        Console.WriteLine($"Path: {path}");
        Console.WriteLine($"   Rooted: {Path.IsPathRooted(path)}");
        Console.WriteLine($"   Fully qualified: {Path.IsPathFullyQualified(path)}");
        Console.WriteLine($"   Full path: {Path.GetFullPath(path)}");
        Console.WriteLine();
    }
}
// The example displays the following output when run on a Windows system:
//    Path: C:Documents
//        Rooted: True
//        Fully qualified: False
//        Full path: c:\Users\user1\Documents\projects\path\ispathrooted\Documents
//
//    Path: /Documents
//       Rooted: True
//       Fully qualified: False
//       Full path: c:\Documents
//
//    Path: C:/Documents
//       Rooted: True
//       Fully qualified: True
//       Full path: C:\Documents

IsPathRooted(String)

Source:
Path.Unix.cs
Source:
Path.Unix.cs
Source:
Path.Unix.cs
Source:
Path.Unix.cs

Returns a value indicating whether the specified path string contains a root.

C#
public static bool IsPathRooted(string path);
C#
public static bool IsPathRooted(string? path);

Parameters

path
String

The path to test.

Returns

true if path contains a root; otherwise, false.

Exceptions

.NET Framework and .NET Core versions older than 2.1: path contains one or more of the invalid characters defined in GetInvalidPathChars().

Examples

The following example demonstrates how the IsPathRooted method can be used to test three strings.

C#
string fileName = @"C:\mydir\myfile.ext";
string UncPath = @"\\myPc\mydir\myfile";
string relativePath = @"mydir\sudir\";
bool result;

result = Path.IsPathRooted(fileName);
Console.WriteLine("IsPathRooted('{0}') returns {1}",
    fileName, result);

result = Path.IsPathRooted(UncPath);
Console.WriteLine("IsPathRooted('{0}') returns {1}",
    UncPath, result);

result = Path.IsPathRooted(relativePath);
Console.WriteLine("IsPathRooted('{0}') returns {1}",
    relativePath, result);

// This code produces output similar to the following:
//
// IsPathRooted('C:\mydir\myfile.ext') returns True
// IsPathRooted('\\myPc\mydir\myfile') returns True
// IsPathRooted('mydir\sudir\') returns False

Remarks

The IsPathRooted method returns true if the first character is a directory separator character such as "\", or if the path starts with a drive letter and colon (:). For example, it returns true for path strings such as "\MyDir\MyFile.txt", "C:\MyDir", or "C:MyDir". It returns false for path strings such as "MyDir".

This method does not verify that the path or file name exists.

For a list of common I/O tasks, see Common I/O Tasks.

See also

Applies to

Product Versions

IsPathRooted(ReadOnlySpan<Char>)

Source:
Path.Unix.cs
Source:
Path.Unix.cs
Source:
Path.Unix.cs
Source:
Path.Unix.cs

Returns a value that indicates whether the specified character span that represents a file path contains a root.

C#
public static bool IsPathRooted(ReadOnlySpan<char> path);

Parameters

path
ReadOnlySpan<Char>

The path to test.

Returns

true if path contains a root; otherwise, false.

See also

Applies to

Product Versions