Path.IsPathRooted Methode

Definition

Gibt einen Wert zurück, der angibt, ob ein Dateipfad einen Stamm enthält.

Überlädt

IsPathRooted(ReadOnlySpan<Char>)

Gibt einen Wert zurück, der angibt, ob die angegebene Zeichenspanne zur Darstellung eines Dateipfads einen Stamm enthält.

IsPathRooted(String)

Gibt einen Wert zurück, der angibt, ob die angegebene Pfadzeichenfolge einen Stamm enthält.

Hinweise

Ein Rootpfad ist ein Dateipfad, der auf ein bestimmtes Laufwerk oder einen UNC-Pfad festgelegt ist. Es steht im Gegensatz zu einem Pfad, der relativ zum aktuellen Laufwerk oder Arbeitsverzeichnis ist. Auf Windows-Systemen beginnt ein rooter Pfad beispielsweise mit einem umgekehrten Schrägstrich (z. B. "\Documents") oder einem Laufwerkbuchstaben und Doppelpunkt (z. B. "C:Documents").

Beachten Sie, dass Rootpfade entweder absolut (d. h. voll qualifiziert) oder relativ sein können. Ein absoluter Rootpfad ist ein vollqualifizierter Pfad vom Stamm eines Laufwerks zu einem bestimmten Verzeichnis. Ein relativer Rootpfad gibt ein Laufwerk an, sein vollqualifizierter Pfad wird jedoch für das aktuelle Verzeichnis aufgelöst. Der Unterschied wird im folgenden Beispiel veranschaulicht.

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>)

Quelle:
Path.Unix.cs
Quelle:
Path.Unix.cs
Quelle:
Path.Unix.cs

Gibt einen Wert zurück, der angibt, ob die angegebene Zeichenspanne zur Darstellung eines Dateipfads einen Stamm enthält.

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

Parameter

path
ReadOnlySpan<Char>

Der zu testende Pfad.

Gibt zurück

true, wenn path einen Stamm enthält, andernfalls false.

Weitere Informationen

Gilt für:

IsPathRooted(String)

Quelle:
Path.Unix.cs
Quelle:
Path.Unix.cs
Quelle:
Path.Unix.cs

Gibt einen Wert zurück, der angibt, ob die angegebene Pfadzeichenfolge einen Stamm enthält.

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

Parameter

path
String

Der zu testende Pfad.

Gibt zurück

true, wenn path einen Stamm enthält, andernfalls false.

Ausnahmen

.NET Framework- und .NET Core-Versionen älter als 2.1: path enthält mindestens eins der ungültigen Zeichen, die in GetInvalidPathChars()definiert sind.

Beispiele

Im folgenden Beispiel wird veranschaulicht, wie die IsPathRooted Methode zum Testen von drei Zeichenfolgen verwendet werden kann.

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

Hinweise

Die IsPathRooted Methode gibt zurück true , wenn das erste Zeichen ein Verzeichnistrennzeichen wie "\" ist oder wenn der Pfad mit einem Laufwerkbuchstaben und Doppelpunkt (:)) beginnt. Beispielsweise wird für path Zeichenfolgen wie "\\MyDir\MyFile.txt", "C:\MyDir" oder "C:MyDir" zurückgegebentrue. Es wird für path Zeichenfolgen wie "MyDir" zurückgegebenfalse.

Diese Methode überprüft nicht, ob der Pfad oder der Dateiname vorhanden ist.

Eine Liste allgemeiner E/A-Aufgaben finden Sie unter Allgemeine E/A-Aufgaben.

Weitere Informationen

Gilt für: