Note

Access to this page requires authorization. You can try signing in or .

Access to this page requires authorization. You can try .

Path.IsPathRooted Method

Definition

Namespace:
System.IO
Assemblies:
mscorlib.dll, System.Runtime.Extensions.dll
Assemblies:
netstandard.dll, System.Runtime.dll
Assembly:
System.Runtime.Extensions.dll
Assembly:
mscorlib.dll
Assembly:
netstandard.dll

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

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

Overloads

Name Description
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.

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
Imports System.IO

Module Program
 Public Sub Main()
 Dim relative1 As String = "C:Documents" 
 ShowPathInfo(relative1)

 Dim relative2 As String = "C:Documents" 
 ShowPathInfo(relative2)

 Dim absolute As String = "C:/Documents"
 ShowPathInfo(absolute)
 End Sub

 Private Sub ShowPathInfo(filepath As String)
 Console.WriteLine($"Path: {filepath}")
 Console.WriteLine($" Rooted: {Path.IsPathRooted(filepath)}")
 Console.WriteLine($" Fully qualified: {Path.IsPathFullyQualified(filepath)}")
 Console.WriteLine($" Full path: {Path.GetFullPath(filepath)}")
 Console.WriteLine()
 End Sub
End Module
' 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
Source:
Path.Unix.cs

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

public:
 static bool IsPathRooted(System::String ^ path);
public static bool IsPathRooted(string path);
public static bool IsPathRooted(string? path);
static member IsPathRooted : string -> bool
Public Shared Function IsPathRooted (path As String) As Boolean

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.

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
Dim fileName As String = "C:\mydir\myfile.ext"
Dim UncPath As String = "\\myPc\mydir\myfile"
Dim relativePath As String = "mydir\sudir\"
Dim result As Boolean

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

IsPathRooted(ReadOnlySpan<Char>)

Source:
Path.Unix.cs
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.

public:
 static bool IsPathRooted(ReadOnlySpan<char> path);
public static bool IsPathRooted(ReadOnlySpan<char> path);
static member IsPathRooted : ReadOnlySpan<char> -> bool
Public Shared Function IsPathRooted (path As ReadOnlySpan(Of Char)) As Boolean

Parameters

path
ReadOnlySpan<Char>

The path to test.

Returns

true if path contains a root; otherwise, false.

See also

Applies to


Feedback

Was this page helpful?