Path.GetPathRoot 方法

定義

多載

GetPathRoot(String)

從指定字串包含的路徑中取得根目錄資訊。

GetPathRoot(ReadOnlySpan<Char>)

從所指定字元範圍中包含的路徑取得根目錄資訊。

GetPathRoot(String)

來源:
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

字串,其包含從中取得根目錄資訊的路徑。

傳回

path 的根目錄 (如果其為根目錄)。

-或-

如果 path 不包含根目錄資訊,則為 Empty

-或-

如果 pathnull 或實際上是空的,則為 null

例外狀況

.NET Framework 和 2.1 之前的 .NET Core 版本:path包含中GetInvalidPathChars()定義的一或多個無效字元。

-或-

僅限 .NET Framework:已將 Empty 傳遞至 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:\'
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回 。

此方法傳回之字串的可能模式如下:

  • nullpath (為 null 或空字串) 。

  • 空字串 (path 指定目前磁碟驅動器或磁碟區) 的相對路徑。

  • “/” (Unix: path 在目前磁碟驅動器上指定絕對路徑) 。

  • “X:” (Windows: path 在磁碟驅動器上指定相對路徑,其中 X 代表磁碟驅動器或磁碟區字母) 。

  • “X:\” (Windows: path 在指定的磁碟驅動器上指定絕對路徑) 。

  • “\\ComputerName\SharedFolder” (Windows:UNC 路徑) 。

  • “\\?\C:” (Windows:.NET Core 1.1 和更新版本中支援的 DOS 裝置路徑,以及 .NET Framework 4.6.2 和更新版本) 。

如需 Windows 上檔案路徑的詳細資訊,請參閱 Windows 系統上的檔案路徑格式。 如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

GetPathRoot(ReadOnlySpan<Char>)

來源:
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 的根目錄。

備註

這個方法不會驗證路徑或檔案是否存在。

不同於字串多載,這個方法不會正規化目錄分隔符。

ReadOnlySpan<System.Char>如果:

此方法傳回之唯讀字元範圍的可能模式如下:

  • ReadOnlySpan<T>.Emptypath (為 ReadOnlySpan<T>.Empty

  • ReadOnlySpan<T>.Emptypath (指定目前磁碟驅動器或磁碟區) 上的相對路徑。

  • “/” (Unix: path 在目前磁碟驅動器上指定絕對路徑) 。

  • “X:” (Windows: path 在磁碟驅動器上指定相對路徑,其中 X 代表磁碟驅動器或磁碟區字母) 。

  • “X:\” (Windows: path 在指定的磁碟驅動器上指定絕對路徑) 。

  • “\\ComputerName\SharedFolder” (Windows:UNC 路徑) 。

  • “\\?\C:” (Windows:.NET Core 1.1 和更新版本中支援的 DOS 裝置路徑,以及 .NET Framework 4.6.2 和更新版本) 。

如需 Windows 上檔案路徑的詳細資訊,請參閱 Windows 系統上的檔案路徑格式。 如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於