Path.GetPathRoot 方法

定義

多載

GetPathRoot(ReadOnlySpan<Char>)

從所指定字元範圍中包含的路徑取得根目錄資訊。Gets the root directory information from the path contained in the specified character span.

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.

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)

參數

path
ReadOnlySpan<Char>

字元的唯讀範圍,其包含從中取得根目錄資訊的路徑。A read-only span of characters containing the path from which to obtain root directory information.

傳回

ReadOnlySpan<Char>

字元的唯讀範圍,其包含 path 的根目錄。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.

ReadOnlySpan<System.Char>如果有下列情況,則為「有效的空」: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>.EmptyReadOnlySpan<T>.Empty (path was ReadOnlySpan<T>.Empty.

  • ReadOnlySpan<T>.Empty (path 指定目前磁片磁碟機或磁片區) 的相對路徑。ReadOnlySpan<T>.Empty (path specified a relative path on the current drive or volume).

  • "/" (Unix: path 指定目前磁片磁碟機) 的絕對路徑。"/" (Unix: path specified an absolute path on the current drive).

  • "X:" (Windows: path 指定磁片磁碟機上的相對路徑,其中 X 代表磁片磁碟機或磁片區字母) 。"X:" (Windows: path specified a relative path on a drive, where X represents a drive or volume letter).

  • "X: " (Windows: path 指定指定磁片磁碟機上的絕對路徑) 。"X:" (Windows: path specified an absolute path on a given drive).

  • " \ \ComputerName\SharedFolder" (Windows:) 的 UNC 路徑。"\\ComputerName\SharedFolder" (Windows: a UNC path).

  • " \ \ ? \C:" (Windows:在 .net Core 1.1 和更新版本中支援的 DOS 裝置路徑,以及 .NET Framework 4.6.2 和更新版本) 。"\\?\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).

如需 Windows 上檔案路徑的詳細資訊,請參閱 windows 系統上的檔案路徑格式For more information on file paths on Windows, see File path formats on Windows systems. 如需一般 i/o 工作的清單,請參閱 一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

另請參閱

適用於

GetPathRoot(String)

從指定字串包含的路徑中取得根目錄資訊。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

參數

path
String

字串,其包含從中取得根目錄資訊的路徑。A string containing the path from which to obtain root directory information.

傳回

String

path 的根目錄 (如果其為根目錄)。The root directory of path if it is rooted.

-或--or- 如果 path 不包含根目錄資訊,則為 EmptyEmpty if path does not contain root directory information.

-或--or- 如果 pathnull 或實際上是空的,則為 nullnull if path is null or is effectively empty.

例外狀況

僅限 .NET Framework:path 包含一或多個 GetInvalidPathChars() 中定義的無效字元。.NET Framework only: path contains one or more of the invalid characters defined in GetInvalidPathChars().

-或--or-

僅限 .NET Framework:已將 Empty 傳遞至 path.NET Framework only: Empty was passed to path.

範例

下列範例示範如何使用 GetPathRoot 方法。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:\'

備註

這個方法不會驗證路徑或檔案是否存在。This method does not verify that the path or file exists.

這個方法會將目錄分隔符號標準化。This method will normalize directory separators.

如果有下列情況,字串就會「有效地空」:A string is "effectively empty" if:

  • 在 Windows 中, IsEmpty 這個字串的呼叫 true 會傳回,或其所有字元都是空格 ( ' ' ) 。In Windows, calling IsEmpty on this string returns true, or all its characters are spaces (' ').
  • 在 Unix 中, IsNullOrEmpty 此字串的呼叫會傳回 trueIn Unix, calling IsNullOrEmpty on this string returns true.

這個方法所傳回之字串的可能模式如下:Possible patterns for the string returned by this method are as follows:

  • null (為 path null 或空的字串) 。null (path was null or an empty string).

  • 空字串 (path 指定目前磁片磁碟機或磁片區) 的相對路徑。An empty string (path specified a relative path on the current drive or volume).

  • "/" (Unix: path 指定目前磁片磁碟機) 的絕對路徑。"/" (Unix: path specified an absolute path on the current drive).

  • "X:" (Windows: path 指定磁片磁碟機上的相對路徑,其中 X 代表磁片磁碟機或磁片區字母) 。"X:" (Windows: path specified a relative path on a drive, where X represents a drive or volume letter).

  • "X: " (Windows: path 指定指定磁片磁碟機上的絕對路徑) 。"X:" (Windows: path specified an absolute path on a given drive).

  • " \ \ComputerName\SharedFolder" (Windows:) 的 UNC 路徑。"\\ComputerName\SharedFolder" (Windows: a UNC path).

  • " \ \ ? \C:" (Windows:在 .net Core 1.1 和更新版本中支援的 DOS 裝置路徑,以及 .NET Framework 4.6.2 和更新版本) 。"\\?\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).

如需 Windows 上檔案路徑的詳細資訊,請參閱 windows 系統上的檔案路徑格式For more information on file paths on Windows, see File path formats on Windows systems. 如需一般 i/o 工作的清單,請參閱 一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

另請參閱

適用於