Path.IsPathRooted Metoda

Definice

Vrátí hodnotu, která označuje, zda cesta k souboru obsahuje kořen.

Přetížení

IsPathRooted(ReadOnlySpan<Char>)

Vrátí hodnotu, která označuje, zda zadaný znak rozsah, který představuje cestu k souboru obsahuje kořen.

IsPathRooted(String)

Vrátí hodnotu určující, zda zadaný řetězec cesty obsahuje kořen.

Poznámky

Kořenová cesta je cesta k souboru, která je pevně nastavena na konkrétní jednotku nebo cestu UNC; je v kontrastu s cestou, která je relativní k aktuální jednotce nebo pracovnímu adresáři. Například v systémech Windows kořenová cesta začíná zpětným lomítkem (například "\Documents") nebo písmenem jednotky a dvojtečkami (například "C:Documents").

Všimněte si, že kořenové cesty mohou být buď absolutní (tj. plně kvalifikované), nebo relativní. Absolutní kořenová cesta je plně kvalifikovaná cesta z kořenového adresáře jednotky do konkrétního adresáře. Relativní kořenová cesta určuje jednotku, ale její plně kvalifikovaná cesta se přeloží na aktuální adresář. Následující příklad znázorňuje rozdíl.

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

Zdroj:
Path.Unix.cs
Zdroj:
Path.Unix.cs
Zdroj:
Path.Unix.cs

Vrátí hodnotu, která označuje, zda zadaný znak rozsah, který představuje cestu k souboru obsahuje kořen.

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>

Cesta k testování.

Návraty

true pokud path obsahuje kořen, jinak falsehodnota .

Viz také

Platí pro

IsPathRooted(String)

Zdroj:
Path.Unix.cs
Zdroj:
Path.Unix.cs
Zdroj:
Path.Unix.cs

Vrátí hodnotu určující, zda zadaný řetězec cesty obsahuje kořen.

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

Cesta k testování.

Návraty

true pokud path obsahuje kořen, jinak falsehodnota .

Výjimky

Verze .NET Framework a .NET Core starší než 2.1: path obsahují jeden nebo více neplatných znaků definovaných v GetInvalidPathChars().

Příklady

Následující příklad ukazuje, jak lze metodu IsPathRooted použít k testování tří řetězců.

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

Poznámky

Metoda IsPathRooted vrátí true , pokud první znak je znak oddělovače adresáře, například "\", nebo pokud cesta začíná písmenem jednotky a dvojtečka (:). Vrátí true například řetězce path jako "\\MyDir\MyFile.txt", "C:\MyDir" nebo "C:MyDir". Vrátí false se pro path řetězce, jako je například "MyDir".

Tato metoda neověřuje, zda cesta nebo název souboru existuje.

Seznam běžných vstupně-výstupních úloh najdete v tématu Běžné vstupně-výstupní úlohy.

Viz také

Platí pro