Path.IsPathRooted Metoda

Definicja

Zwraca wartość wskazującą, czy ścieżka pliku zawiera katalog główny.

Przeciążenia

IsPathRooted(ReadOnlySpan<Char>)

Zwraca wartość wskazującą, czy określony zakres znaków reprezentujący ścieżkę pliku zawiera katalog główny.

IsPathRooted(String)

Zwraca wartość wskazującą, czy określony ciąg ścieżki zawiera katalog główny.

Uwagi

Ścieżka rooted to ścieżka pliku, która jest stała dla określonego dysku lub ścieżki UNC; kontrastuje ze ścieżką względną dla bieżącego dysku lub katalogu roboczego. Na przykład w systemach Windows ścieżka rooted zaczyna się ukośnikiem odwrotnym (na przykład "\Documents") lub literą dysku i dwukropkiem (na przykład "C:Documents").

Należy pamiętać, że ścieżki rooted mogą być bezwzględne (czyli w pełni kwalifikowane) lub względne. Bezwzględna ścieżka rooted jest w pełni kwalifikowaną ścieżką z katalogu głównego dysku do określonego katalogu. Względna ścieżka rooted określa dysk, ale jego w pełni kwalifikowana ścieżka jest rozpoznawana względem bieżącego katalogu. Poniższy przykład ilustruje różnicę.

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(ReadOnlySpan<Char>)

Źródło:
Path.Unix.cs
Źródło:
Path.Unix.cs
Źródło:
Path.Unix.cs

Zwraca wartość wskazującą, czy określony zakres znaków reprezentujący ścieżkę pliku zawiera katalog główny.

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

Parametry

path
ReadOnlySpan<Char>

Ścieżka do testowania.

Zwraca

true jeśli path zawiera katalog główny; w przeciwnym razie false.

Zobacz też

Dotyczy

IsPathRooted(String)

Źródło:
Path.Unix.cs
Źródło:
Path.Unix.cs
Źródło:
Path.Unix.cs

Zwraca wartość wskazującą, czy określony ciąg ścieżki zawiera katalog główny.

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

Parametry

path
String

Ścieżka do testowania.

Zwraca

true jeśli path zawiera katalog główny; w przeciwnym razie false.

Wyjątki

.NET Framework i .NET Core w wersjach starszych niż 2.1: path zawiera co najmniej jeden nieprawidłowy znak zdefiniowany w programie GetInvalidPathChars().

Przykłady

W poniższym przykładzie pokazano, jak IsPathRooted można użyć metody do testowania trzech ciągów.

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

Uwagi

Metoda IsPathRooted zwraca, true jeśli pierwszy znak jest znakiem separatora katalogu, takim jak "\", lub jeśli ścieżka rozpoczyna się literą dysku i dwukropkiem (:). Na przykład zwraca truepath on ciągi, takie jak "\\MyDir\MyFile.txt", "C:\MyDir" lub "C:MyDir". Zwraca false on ciągi path , takie jak "MyDir".

Ta metoda nie sprawdza, czy ścieżka lub nazwa pliku istnieje.

Aby uzyskać listę typowych zadań we/wy, zobacz Typowe zadania we/wy.

Zobacz też

Dotyczy