Path.IsPathRooted Methode

Definitie

Retourneert een waarde die aangeeft of een bestandspad een hoofdmap bevat.

Overloads

Name Description
IsPathRooted(ReadOnlySpan<Char>)

Retourneert een waarde die aangeeft of het opgegeven tekenbereik dat een bestandspad vertegenwoordigt een hoofdmap bevat.

IsPathRooted(String)

Retourneert een waarde die aangeeft of de opgegeven padtekenreeks een hoofdtekenreeks bevat.

Opmerkingen

Een geroot pad is bestandspad dat is vastgezet aan een specifiek station of UNC-pad; het contrasteert met een pad dat relatief is ten opzichte van het huidige station of de huidige werkmap. Op Windows systemen begint een geroot pad bijvoorbeeld met een backslash (bijvoorbeeld '\Documents') of een stationsletter en dubbele punt (bijvoorbeeld 'C:Documents').

Houd er rekening mee dat geroote paden absoluut (dat wil weten, volledig gekwalificeerd) of relatief kunnen zijn. Een absoluut geroot pad is een volledig gekwalificeerd pad van de hoofdmap van een station naar een specifieke map. Een relatief geroot pad specificeert een station, maar het volledig gekwalificeerde pad wordt omgezet op basis van de huidige map. In het volgende voorbeeld ziet u het verschil.

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

Retourneert een waarde die aangeeft of het opgegeven tekenbereik dat een bestandspad vertegenwoordigt een hoofdmap bevat.

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

Parameters

path
ReadOnlySpan<Char>

Het pad om te testen.

Retouren

trueals path het een hoofdmap bevat; anders. false

Zie ook

Van toepassing op

IsPathRooted(String)

Retourneert een waarde die aangeeft of de opgegeven padtekenreeks een hoofdtekenreeks bevat.

public:
 static bool IsPathRooted(System::String ^ path);
public static bool IsPathRooted(string path);
static member IsPathRooted : string -> bool
Public Shared Function IsPathRooted (path As String) As Boolean

Parameters

path
String

Het pad om te testen.

Retouren

trueals path het een hoofdmap bevat; anders. false

Uitzonderingen

.NET Framework en .NET Core-versies ouder dan 2.1: path bevat een of meer ongeldige tekens die zijn gedefinieerd in GetInvalidPathChars().

Voorbeelden

In het volgende voorbeeld ziet u hoe de IsPathRooted methode kan worden gebruikt om drie tekenreeksen te testen.

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

Opmerkingen

De IsPathRooted methode retourneert true als het eerste teken een adreslijstscheidingsteken is, zoals '\', of als het pad begint met een stationsletter en dubbele punt (:). Het retourneert true bijvoorbeeld tekenreeksen path zoals "\\MyDir\MyFile.txt", "C:\MyDir" of "C:MyDir". Het retourneert false tekenreeksen path zoals 'MyDir'.

Met deze methode wordt niet gecontroleerd of het pad of de bestandsnaam bestaat.

Zie Algemene I/O-taken voor een lijst met algemene I/O-taken.

Zie ook

Van toepassing op