Path.IsPathRooted 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳一個值,指示檔案路徑是否包含根節點。
多載
| 名稱 | Description |
|---|---|
| IsPathRooted(String) |
傳回值,指出指定的路徑字串是否包含根目錄。 |
| IsPathRooted(ReadOnlySpan<Char>) |
傳回值,這個值表示代表檔案路徑的指定字元範圍是否包含根目錄。 |
備註
根路徑是指固定在特定磁碟或 UNC 路徑上的檔案路徑;它與相對於目前磁碟或工作目錄的路徑形成對比。 例如,在 Windows 系統中,根節點路徑以反斜線(例如「\Documents」)或磁碟字母與冒號(例如「C:Documents」)開頭。
請注意,根路徑可以是絕對路徑(即完全限定路徑)或相對路徑。 絕對根路徑是指從硬碟根節點到特定目錄的完整合格路徑。 相對根路徑指定磁碟機,但其完全限定路徑會根據目前目錄解析。 下列範例會說明其間的差異。
using System;
using System.IO;
class Program
{
static void Main()
{
string relative1 = "C:Documents";
ShowPathInfo(relative1);
string relative2 = "/Documents";
ShowPathInfo(relative2);
string absolute = "C:/Documents";
ShowPathInfo(absolute);
}
private static void ShowPathInfo(string path)
{
Console.WriteLine($"Path: {path}");
Console.WriteLine($" Rooted: {Path.IsPathRooted(path)}");
Console.WriteLine($" Fully qualified: {Path.IsPathFullyQualified(path)}");
Console.WriteLine($" Full path: {Path.GetFullPath(path)}");
Console.WriteLine();
}
}
// The example displays the following output when run on a Windows system:
// Path: C:Documents
// Rooted: True
// Fully qualified: False
// Full path: c:\Users\user1\Documents\projects\path\ispathrooted\Documents
//
// Path: /Documents
// Rooted: True
// Fully qualified: False
// Full path: c:\Documents
//
// Path: C:/Documents
// Rooted: True
// Fully qualified: True
// Full path: C:\Documents
Imports System.IO
Module Program
Public Sub Main()
Dim relative1 As String = "C:Documents"
ShowPathInfo(relative1)
Dim relative2 As String = "C:Documents"
ShowPathInfo(relative2)
Dim absolute As String = "C:/Documents"
ShowPathInfo(absolute)
End Sub
Private Sub ShowPathInfo(filepath As String)
Console.WriteLine($"Path: {filepath}")
Console.WriteLine($" Rooted: {Path.IsPathRooted(filepath)}")
Console.WriteLine($" Fully qualified: {Path.IsPathFullyQualified(filepath)}")
Console.WriteLine($" Full path: {Path.GetFullPath(filepath)}")
Console.WriteLine()
End Sub
End Module
' The example displays the following output when run on a Windows system:
' Path: C:Documents
' Rooted: True
' Fully qualified: False
' Full path: c:\Users\user1\Documents\projects\path\ispathrooted\Documents
'
' Path: /Documents
' Rooted: True
' Fully qualified: False
' Full path: c:\Documents
'
' Path: C:/Documents
' Rooted: True
' Fully qualified: True
' Full path: C:\Documents
IsPathRooted(String)
- 來源:
- Path.Unix.cs
- 來源:
- Path.Unix.cs
- 來源:
- Path.Unix.cs
- 來源:
- Path.Unix.cs
- 來源:
- Path.Unix.cs
傳回值,指出指定的路徑字串是否包含根目錄。
public:
static bool IsPathRooted(System::String ^ path);
public static bool IsPathRooted(string path);
public static bool IsPathRooted(string? path);
static member IsPathRooted : string -> bool
Public Shared Function IsPathRooted (path As String) As Boolean
參數
- path
- String
測試的路徑。
傳回
true若path包含根;否則,。 false
例外狀況
.NET Framework 與 .NET Core 版本早於 2.1: path 包含一個或多個定義於 GetInvalidPathChars().
範例
以下範例說明該方法如何 IsPathRooted 用來測試三條字串。
string fileName = @"C:\mydir\myfile.ext";
string UncPath = @"\\myPc\mydir\myfile";
string relativePath = @"mydir\sudir\";
bool result;
result = Path.IsPathRooted(fileName);
Console.WriteLine("IsPathRooted('{0}') returns {1}",
fileName, result);
result = Path.IsPathRooted(UncPath);
Console.WriteLine("IsPathRooted('{0}') returns {1}",
UncPath, result);
result = Path.IsPathRooted(relativePath);
Console.WriteLine("IsPathRooted('{0}') returns {1}",
relativePath, result);
// This code produces output similar to the following:
//
// IsPathRooted('C:\mydir\myfile.ext') returns True
// IsPathRooted('\\myPc\mydir\myfile') returns True
// IsPathRooted('mydir\sudir\') returns False
Dim fileName As String = "C:\mydir\myfile.ext"
Dim UncPath As String = "\\myPc\mydir\myfile"
Dim relativePath As String = "mydir\sudir\"
Dim result As Boolean
result = Path.IsPathRooted(fileName)
Console.WriteLine("IsPathRooted('{0}') returns {1}", fileName, result)
result = Path.IsPathRooted(UncPath)
Console.WriteLine("IsPathRooted('{0}') returns {1}", UncPath, result)
result = Path.IsPathRooted(relativePath)
Console.WriteLine("IsPathRooted('{0}') returns {1}", relativePath, result)
' This code produces output similar to the following:
'
' IsPathRooted('C:\mydir\myfile.ext') returns True
' IsPathRooted('\\myPc\mydir\myfile') returns True
' IsPathRooted('mydir\sudir\') returns False
備註
如果第一個字元是目錄分隔符(如「\」),或路徑以磁碟機代號和冒號(:) 開頭,該 IsPathRooted 方法會回傳 true 。 例如,當字true串如「\\MyDir\MyFile.txt」、「C:\MyDir」或「C:MyDir」時,會回傳path。 對於false像「MyDir」這類字串,它會回傳path。
此方法無法驗證路徑或檔案名稱的存在。
關於常見 I/O 任務的清單,請參見 Common I/O 任務。
另請參閱
適用於
IsPathRooted(ReadOnlySpan<Char>)
- 來源:
- Path.Unix.cs
- 來源:
- Path.Unix.cs
- 來源:
- Path.Unix.cs
- 來源:
- Path.Unix.cs
- 來源:
- Path.Unix.cs
傳回值,這個值表示代表檔案路徑的指定字元範圍是否包含根目錄。
public:
static bool IsPathRooted(ReadOnlySpan<char> path);
public static bool IsPathRooted(ReadOnlySpan<char> path);
static member IsPathRooted : ReadOnlySpan<char> -> bool
Public Shared Function IsPathRooted (path As ReadOnlySpan(Of Char)) As Boolean
參數
- path
- ReadOnlySpan<Char>
測試的路徑。
傳回
true若path包含根;否則,。 false