Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
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. |
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
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
The path to test.
true
if path
contains a root; otherwise, false
.
.NET Framework and .NET Core versions older than 2.1: path
contains one or more of the invalid characters defined in GetInvalidPathChars().
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
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.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
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
The path to test.
true
if path
contains a root; otherwise, false
.
Product | Versions |
---|---|
.NET | Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Standard | 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in