Path.GetPathRoot Path.GetPathRoot Path.GetPathRoot Path.GetPathRoot Method

Definition

Overloads

GetPathRoot(String) GetPathRoot(String) GetPathRoot(String) GetPathRoot(String)

Gets the root directory information of the specified path.

GetPathRoot(ReadOnlySpan<Char>) GetPathRoot(ReadOnlySpan<Char>) GetPathRoot(ReadOnlySpan<Char>) GetPathRoot(ReadOnlySpan<Char>)

Gets the root directory information from the path contained in the specified character span.

GetPathRoot(String) GetPathRoot(String) GetPathRoot(String) GetPathRoot(String)

Gets the root directory information of the specified path.

public:
 static System::String ^ GetPathRoot(System::String ^ path);
public static string GetPathRoot (string path);
static member GetPathRoot : string -> string
Public Shared Function GetPathRoot (path As String) As String

Parameters

path
String String String String

The path from which to obtain root directory information.

Returns

The root directory of path, or null if path is null, or an empty string if path does not contain root directory information.

Exceptions

path contains one or more of the invalid characters defined in GetInvalidPathChars().

-or-

Empty was passed to path.

Examples

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:\'
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:\'

Remarks

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

Possible patterns for the string returned by this method are as follows:

  • An empty string (path specified a relative path on the current drive or volume).

  • "" (path specified an absolute path on the current drive).

  • "X:" (path specified a relative path on a drive, where X represents a drive or volume letter).

  • "X:" (path specified an absolute path on a given drive).

  • "\\ComputerName\SharedFolder" (a UNC path).

  • "\?\C:" (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.

See also

GetPathRoot(ReadOnlySpan<Char>) GetPathRoot(ReadOnlySpan<Char>) GetPathRoot(ReadOnlySpan<Char>) GetPathRoot(ReadOnlySpan<Char>)

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)

Parameters

path
ReadOnlySpan<Char>

The path from which to obtain root directory information.

Returns

A character span containing the root directory of path.

See also

Applies to