Path.IsPathRooted 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回值,指出檔案路徑是否包含根目錄。
多載
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
傳回值,指出指定的路徑字串是否包含根目錄。
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
要測試的路徑。
傳回
如果 path
包含根目錄,則為 true
;否則為 false
。
例外狀況
.NET Framework 和 2.1 之前的 .NET Core 版本: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.ToString() );
result = Path::IsPathRooted( UncPath );
Console::WriteLine( "IsPathRooted('{0}') returns {1}", UncPath, result.ToString() );
result = Path::IsPathRooted( relativePath );
Console::WriteLine( "IsPathRooted('{0}') returns {1}", relativePath, result.ToString() );
// 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
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
(:) 。 例如,它會針對 path
「\\MyDir\MyFile.txt」、“C:\MyDir” 或 “C:MyDir” 等字串傳回 true
。 它會針對 path
「MyDir」 之類的字串傳回 false
。
這個方法不會驗證路徑或檔名是否存在。
如需一般 I/O 工作的清單,請參閱 一般 I/O 工作。
另請參閱
適用於
IsPathRooted(ReadOnlySpan<Char>)
- 來源:
- 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>
要測試的路徑。
傳回
如果 path
包含根目錄,則為 true
;否則為 false
。