Upraviť

Zdieľať cez


Path.GetPathRoot Method

Definition

Overloads

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.

GetPathRoot(String)

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

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

Parameters

path
String

A string containing the path from which to obtain root directory information.

Returns

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.

Exceptions

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

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 exists.

This method will normalize directory separators.

A string is "effectively empty" if:

  • In Windows, calling IsEmpty on this string returns true, or all its characters are spaces (' ').
  • In Unix, calling IsNullOrEmpty on this string returns 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.

See also

Applies to

GetPathRoot(ReadOnlySpan<Char>)

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

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>

A read-only span of characters containing the path from which to obtain root directory information.

Returns

A read-only span of characters containing the root directory of path.

Remarks

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:

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.

See also

Applies to