Path.GetPathRoot 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| GetPathRoot(String) |
從指定字串中的路徑取得根目錄資訊。 |
| GetPathRoot(ReadOnlySpan<Char>) |
從指定字元區間內的路徑取得根目錄資訊。 |
GetPathRoot(String)
- 來源:
- Path.Unix.cs
- 來源:
- Path.Unix.cs
- 來源:
- Path.Unix.cs
- 來源:
- Path.Unix.cs
- 來源:
- Path.Unix.cs
從指定字串中的路徑取得根目錄資訊。
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
包含取得根目錄資訊路徑的字串。
傳回
如果是 root,則是 的 path 根目錄。
-或-
Empty if path 不包含根目錄資訊。
-或-
null 若 path 或 null 實際上為空。
例外狀況
.NET Framework 與 .NET Core 版本早於 2.1: path 包含一個或多個定義於 GetInvalidPathChars().
-或-
僅 Empty .NET Framework 被傳遞至 path。
範例
以下範例展示了該 GetPathRoot 方法的使用方式。
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:\'
備註
此方法無法驗證路徑或檔案的存在。
此方法將使目錄分隔符進行正規化。
若字串「實質上為空」,則稱為「實質空」:
- 在 Windows 中,呼叫
IsEmpty此字串會回傳true,或其所有字元皆為空格(' ')。 - 在 Unix 中,呼叫 IsNullOrEmpty 此字串會返回
true。
此方法回傳的串串可能模式如下:
null(path是空字串或空字串)。空字串(
path指定當前磁碟機或磁碟區的相對路徑)。「/」(Unix:
path指定了當前硬碟上的絕對路徑)。「X:」(Windows:
path指定磁碟機上的相對路徑, X 代表磁碟機或磁碟區代號)。「X:\」(Windows:
path指定特定磁碟上的絕對路徑)。「\\ComputerName\SharedFolder」(Windows:UNC 路徑)。
「\\?\C:」(Windows:DOS 裝置路徑,支援於 .NET 版本及 .NET Framework 4.6.2 及以上版本)。
欲了解更多關於 Windows 檔案路徑的資訊,請參閱 Windows 系統上的檔案路徑格式。 關於常見 I/O 任務的清單,請參見 Common I/O 任務。
另請參閱
適用於
GetPathRoot(ReadOnlySpan<Char>)
- 來源:
- Path.Unix.cs
- 來源:
- Path.Unix.cs
- 來源:
- Path.Unix.cs
- 來源:
- Path.Unix.cs
- 來源:
- Path.Unix.cs
從指定字元區間內的路徑取得根目錄資訊。
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>
一個只讀的字元區段,包含取得根目錄資訊的路徑。
傳回
一個只讀的字元範圍,包含 的 path根目錄。
備註
此方法無法驗證路徑或檔案的存在。
與字串過載不同,此方法不會正規化目錄分隔符。
若 A ReadOnlySpan<System.Char> 為「實質空」,則稱為「實質空」:
- 在 Windows 中,呼叫 ReadOnlySpan<T>.IsEmpty 此字元區段時會回傳
true,或所有字元皆為空格(' ')。 - 在 Unix 中,呼叫 ReadOnlySpan<T>.IsEmpty 此字元區塊會返回
true。
此方法回傳的唯讀字元區間可能模式如下:
ReadOnlySpan<T>.Empty (
path曾是 ReadOnlySpan<T>.Empty。ReadOnlySpan<T>.Empty (
path指定電流驅動器或磁碟區上的相對路徑)。「/」(Unix:
path指定了當前硬碟上的絕對路徑)。「X:」(Windows:
path指定磁碟機上的相對路徑, X 代表磁碟機或磁碟區代號)。「X:\」(Windows:
path指定特定磁碟上的絕對路徑)。「\\ComputerName\SharedFolder」(Windows:UNC 路徑)。
「\\?\C:」(Windows:DOS 裝置路徑,支援於 .NET 版本及 .NET Framework 4.6.2 及以上版本)。
欲了解更多關於 Windows 檔案路徑的資訊,請參閱 Windows 系統上的檔案路徑格式。 關於常見 I/O 任務的清單,請參見 Common I/O 任務。