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.
GetPathRoot(String) |
Gets the root directory information from the path contained in the specified string. |
GetPathRoot(ReadOnlySpan<Char>) |
Gets the root directory information from the path contained in the specified character span. |
Gets the root directory information from the path contained in the specified string.
public:
static System::String ^ GetPathRoot(System::String ^ path);
public static string GetPathRoot(string path);
public static string? GetPathRoot(string? path);
static member GetPathRoot : string -> string
Public Shared Function GetPathRoot (path As String) As String
A string containing the path from which to obtain root directory information.
The root directory of path
if it is rooted.
-or-
Empty if path
does not contain root directory information.
-or-
null
if path
is null
or is effectively empty.
.NET Framework and .NET Core versions older than 2.1: path
contains one or more of the invalid characters defined in GetInvalidPathChars().
-or-
.NET Framework only: Empty was passed to path
.
The following example demonstrates a use of the GetPathRoot
method.
string path = @"\mydir\";
string fileName = "myfile.ext";
string fullPath = @"C:\mydir\myfile.ext";
string pathRoot;
pathRoot = Path.GetPathRoot(path);
Console.WriteLine("GetPathRoot('{0}') returns '{1}'",
path, pathRoot);
pathRoot = Path.GetPathRoot(fileName);
Console.WriteLine("GetPathRoot('{0}') returns '{1}'",
fileName, pathRoot);
pathRoot = Path.GetPathRoot(fullPath);
Console.WriteLine("GetPathRoot('{0}') returns '{1}'",
fullPath, pathRoot);
// This code produces output similar to the following:
//
// GetPathRoot('\mydir\') returns '\'
// GetPathRoot('myfile.ext') returns ''
// GetPathRoot('C:\mydir\myfile.ext') returns 'C:\'
Dim pathname As String = "\mydir\"
Dim fileName As String = "myfile.ext"
Dim fullPath As String = "C:\mydir\myfile.ext"
Dim pathnameRoot As String
pathnameRoot = Path.GetPathRoot(pathname)
Console.WriteLine("GetPathRoot('{0}') returns '{1}'", pathname, pathnameRoot)
pathnameRoot = Path.GetPathRoot(fileName)
Console.WriteLine("GetPathRoot('{0}') returns '{1}'", fileName, pathnameRoot)
pathnameRoot = Path.GetPathRoot(fullPath)
Console.WriteLine("GetPathRoot('{0}') returns '{1}'", fullPath, pathnameRoot)
' This code produces output similar to the following:
'
' GetPathRoot('\mydir\') returns '\'
' GetPathRoot('myfile.ext') returns ''
' GetPathRoot('C:\mydir\myfile.ext') returns 'C:\'
This method does not verify that the path or file exists.
This method will normalize directory separators.
A string is "effectively empty" if:
IsEmpty
on this string returns true
, or all its characters are spaces (' ').true
.Possible patterns for the string returned by this method are as follows:
null
(path
was null or an empty string).
An empty string (path
specified a relative path on the current drive or volume).
"/" (Unix: path
specified an absolute path on the current drive).
"X:" (Windows: path
specified a relative path on a drive, where X represents a drive or volume letter).
"X:\" (Windows: path
specified an absolute path on a given drive).
"\ComputerName\SharedFolder" (Windows: a UNC path).
"\?\C:" (Windows: a DOS device path, supported in .NET Core 1.1 and later versions, and in .NET Framework 4.6.2 and later versions).
For more information on file paths on Windows, see File path formats on Windows systems. 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 |
Gets the root directory information from the path contained in the specified character span.
public:
static ReadOnlySpan<char> GetPathRoot(ReadOnlySpan<char> path);
public static ReadOnlySpan<char> GetPathRoot(ReadOnlySpan<char> path);
static member GetPathRoot : ReadOnlySpan<char> -> ReadOnlySpan<char>
Public Shared Function GetPathRoot (path As ReadOnlySpan(Of Char)) As ReadOnlySpan(Of Char)
A read-only span of characters containing the path from which to obtain root directory information.
A read-only span of characters containing the root directory of path
.
This method does not verify that the path or file exists.
Unlike the string overload, this method doesn't normalize directory separators.
A ReadOnlySpan<System.Char>
is "effectively empty" if:
true
, or all its characters are spaces (' ').true
.Possible patterns for the read-only character span returned by this method are as follows:
ReadOnlySpan<T>.Empty (path
was ReadOnlySpan<T>.Empty.
ReadOnlySpan<T>.Empty (path
specified a relative path on the current drive or volume).
"/" (Unix: path
specified an absolute path on the current drive).
"X:" (Windows: path
specified a relative path on a drive, where X represents a drive or volume letter).
"X:\" (Windows: path
specified an absolute path on a given drive).
"\ComputerName\SharedFolder" (Windows: a UNC path).
"\?\C:" (Windows: a DOS device path, supported in .NET Core 1.1 and later versions, and in .NET Framework 4.6.2 and later versions).
For more information on file paths on Windows, see File path formats on Windows systems. For a list of common I/O tasks, see Common I/O Tasks.
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