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 wurzelter Pfad ist Dateipfad , der auf ein bestimmtes Laufwerk oder einen UNC-Pfad behoben ist; es kontrastiert mit einem Pfad, der relativ zum aktuellen Laufwerk oder Arbeitsverzeichnis ist. Bei Windows Systemen beginnt beispielsweise ein wurzelter Pfad mit einem Backslash (z. B. "Dokumente") oder einem Laufwerkbuchstaben und Doppelpunkt (z. B. "\C:Dokumente").

Beachten Sie, dass wurzelige Pfade entweder absolut (also vollqualifizierte) oder relativ sein können. Ein absoluter Stammpfad ist ein vollqualifizierter Pfad aus dem Stamm eines Laufwerks zu einem bestimmten Verzeichnis. Ein relativer wurzelter Pfad gibt ein Laufwerk an, aber sein vollqualifizierter Pfad wird im aktuellen 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>)

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

Boolean

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

Siehe auch

Gilt für:

IsPathRooted(String)

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

Boolean

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

Ausnahmen

.NET Framework- und .NET Core-Versionen, die älter als 2.1 sind: path enthält mindestens ein ungültiges Zeichen, das in GetInvalidPathChars().

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 es sich bei dem ersten Zeichen um ein Verzeichnistrennzeichen wie "\" handelt oder wenn der Pfad mit einem Laufwerkbuchstaben und Doppelpunkt (:)) beginnt. Sie gibt true beispielsweise für path Zeichenfolgen wie "\\MyDir\MyFile.txt", "C:\MyDir" oder "C:MyDir" zurück. Er gibt für path Zeichenfolgen wie "MyDir" zurückfalse.

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

Eine Liste allgemeiner I /O-Aufgaben finden Sie unter "Allgemeine I/O-Aufgaben".

Siehe auch

Gilt für: