Path.IsPathRooted Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Gibt einen Wert zurück, der angibt, ob ein Dateipfad einen Stamm enthält.
Überlädt
| Name | Beschreibung |
|---|---|
| IsPathRooted(String) |
Gibt einen Wert zurück, der angibt, ob die angegebene Pfadzeichenfolge einen Stamm enthält. |
| IsPathRooted(ReadOnlySpan<Char>) |
Gibt einen Wert zurück, der angibt, ob der angegebene Zeichenbereich, der einen Dateipfad darstellt, einen Stamm enthält. |
Hinweise
Ein rooter Pfad ist dateipfad, der auf einem bestimmten Laufwerks- oder UNC-Pfad festgelegt ist. es steht im Gegensatz zu einem Pfad, der relativ zum aktuellen Laufwerk oder Arbeitsverzeichnis ist. Bei Windows Systemen beginnt beispielsweise ein rooter Pfad mit einem umgekehrten Schrägstrich (z. B. "\Documents") oder einem Laufwerkbuchstaben und Doppelpunkt (z. B. "C:Documents").
Beachten Sie, dass roote Pfade entweder absolut (d. h. vollqualifizierte) oder relativ sein können. Ein absoluter Stammpfad ist ein vollqualifizierter Pfad vom Stamm eines Laufwerks zu einem bestimmten Verzeichnis. Ein relativer rooter Pfad gibt ein Laufwerk an, der vollqualifizierte 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(String)
- Quelle:
- Path.Unix.cs
- Quelle:
- Path.Unix.cs
- 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 ein 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);
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 true sie für path Zeichenfolgen wie "\\MyDir\MyFile.txt", "C:\MyDir" oder "C:MyDir" zurückgegeben. Er gibt für false Zeichenfolgen wie "MyDir" zurückpath.
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
- File-Pfadformate auf Windows Systemen
- Datei- und Stream-E/A
- Vorgehensweise: Lesen von Text aus einer Datei
- Vorgehensweise: Schreiben von Text in eine Datei
Gilt für:
IsPathRooted(ReadOnlySpan<Char>)
- Quelle:
- Path.Unix.cs
- Quelle:
- Path.Unix.cs
- Quelle:
- Path.Unix.cs
- Quelle:
- Path.Unix.cs
- Quelle:
- Path.Unix.cs
Gibt einen Wert zurück, der angibt, ob der angegebene Zeichenbereich, der einen Dateipfad darstellt, 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 ein Stamm enthält; andernfalls false.