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.
傳回
字元的唯讀範圍,其包含 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:
- 在 Windows 中,在 ReadOnlySpan<T>.IsEmpty 此字元範圍上呼叫
true
會傳回,或其所有字元都是空格 ( ' ' ) 。In Windows, calling ReadOnlySpan<T>.IsEmpty on this span of characters returnstrue
, or all its characters are spaces (' '). - 在 Unix 中, ReadOnlySpan<T>.IsEmpty 在此字元範圍上呼叫會傳回
true
。In Unix, calling ReadOnlySpan<T>.IsEmpty on this span of characters returnstrue
.
這個方法所傳回之唯讀字元範圍的可能模式如下: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
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.
傳回
path
的根目錄 (如果其為根目錄)。The root directory of path
if it is rooted.
-或--or-
如果 path
不包含根目錄資訊,則為 Empty。Empty if path
does not contain root directory information.
-或--or-
如果 path
為 null
或實際上是空的,則為 null
。null
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, callingIsEmpty
on this string returnstrue
, or all its characters are spaces (' '). - 在 Unix 中, IsNullOrEmpty 此字串的呼叫會傳回
true
。In Unix, calling IsNullOrEmpty on this string returnstrue
.
這個方法所傳回之字串的可能模式如下: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.
另請參閱
- Windows 系統上的檔案路徑格式File path formats on Windows systems
- 檔案和資料流 I/OFile and Stream I/O
- 作法:讀取檔案中的文字How to: Read Text from a File
- 作法:將文字寫入檔案How to: Write Text to a File