Path.IsPathRooted Método

Definição

Retorna um valor que indica se um caminho de arquivo contém uma raiz.

Sobrecargas

IsPathRooted(ReadOnlySpan<Char>)

Retorna um valor que indica se o intervalo de caracteres especificado que representa um caminho de arquivo contém uma raiz.

IsPathRooted(String)

Retorna um valor que indica se a cadeia de caracteres do caminho especificado contém uma raiz.

Comentários

Um caminho com raiz é o caminho do arquivo corrigido para uma unidade específica ou caminho UNC; ele contrasta com um caminho relativo à unidade atual ou ao diretório de trabalho. Por exemplo, em sistemas Windows, um caminho com raiz começa com uma barra invertida (por exemplo, "\Documents") ou uma letra de unidade e dois-pontos (por exemplo, "C:Documents").

Observe que caminhos raiz podem ser absolutos (ou seja, totalmente qualificados) ou relativos. Um caminho com raiz absoluta é um caminho totalmente qualificado da raiz de uma unidade para um diretório específico. Um caminho com raiz relativa especifica uma unidade, mas seu caminho totalmente qualificado é resolvido em relação ao diretório atual. O exemplo a seguir ilustra a diferença.

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

Origem:
Path.Unix.cs
Origem:
Path.Unix.cs
Origem:
Path.Unix.cs

Retorna um valor que indica se o intervalo de caracteres especificado que representa um caminho de arquivo contém uma raiz.

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

Parâmetros

path
ReadOnlySpan<Char>

O caminho a ser testado.

Retornos

true se path contém uma raiz; caso contrário, false.

Confira também

Aplica-se a

IsPathRooted(String)

Origem:
Path.Unix.cs
Origem:
Path.Unix.cs
Origem:
Path.Unix.cs

Retorna um valor que indica se a cadeia de caracteres do caminho especificado contém uma raiz.

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

Parâmetros

path
String

O caminho a ser testado.

Retornos

true se path contém uma raiz; caso contrário, false.

Exceções

.NET Framework e versões do .NET Core anteriores à 2.1: path contém um ou mais dos caracteres inválidos definidos em GetInvalidPathChars().

Exemplos

O exemplo a seguir demonstra como o IsPathRooted método pode ser usado para testar três cadeias de caracteres.

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

Comentários

O IsPathRooted método retornará true se o primeiro caractere for um caractere separador de diretório, como "\", ou se o caminho começar com uma letra da unidade e dois-pontos (:). Por exemplo, ele retorna true para path cadeias de caracteres como "\\MyDir\MyFile.txt", "C:\MyDir" ou "C:MyDir". Ele retorna false para path cadeias de caracteres como "MyDir".

Esse método não verifica se o caminho ou o nome do arquivo existe.

Para obter uma lista de tarefas comuns de E/S, consulte Tarefas comuns de E/S.

Confira também

Aplica-se a