Path.IsPathRooted Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Devolve um valor que indica se um caminho de ficheiro contém uma raiz.
Sobrecargas
| Name | Description |
|---|---|
| IsPathRooted(String) |
Devolve um valor que indica se a cadeia de caminho especificada contém uma raiz. |
| IsPathRooted(ReadOnlySpan<Char>) |
Devolve um valor que indica se o espaço de caracteres especificado que representa um caminho de ficheiro contém uma raiz. |
Observações
Um caminho enraizado é um caminho de ficheiro fixo para um determinado disco ou caminho UNC; Contrasta com um caminho que é relativo ao disco atual ou diretório de trabalho. Por exemplo, em sistemas Windows, um caminho enraizado começa com uma barra diagonal (por exemplo, "\Documents") ou uma letra de drive e dois pontos (por exemplo, "C:Documents").
Note que os caminhos enraizados podem ser absolutos (ou seja, totalmente qualificados) ou relativos. Um caminho raiz absoluto é um caminho totalmente qualificado da raiz de um disco para um diretório específico. Um caminho enraizado relativo especifica uma unidade, mas o seu caminho totalmente qualificado é resolvido contra o 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(String)
- Origem:
- Path.Unix.cs
- Origem:
- Path.Unix.cs
- Origem:
- Path.Unix.cs
- Origem:
- Path.Unix.cs
- Origem:
- Path.Unix.cs
Devolve um valor que indica se a cadeia de caminho especificada 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 testar.
Devoluções
true se path contiver uma raiz; caso contrário, false.
Exceções
.NET Framework e .NET Core versões anteriores à 2.1: path contém um ou mais dos caracteres inválidos definidos em GetInvalidPathChars().
Exemplos
O exemplo seguinte demonstra como o IsPathRooted método pode ser usado para testar três cadeias.
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
Observações
O IsPathRooted método retorna true se o primeiro carácter for um carácter separador de diretório, como "\", ou se o caminho começar com uma letra de unidade e dois pontos (:). Por exemplo, retorna true para path strings como "\\MyDir\MyFile.txt", "C:\MyDir" ou "C:MyDir". Retorna false para path cordas como "MyDir".
Este método não verifica se o caminho ou nome do ficheiro existe.
Para uma lista de tarefas comuns de E/S, consulte Tarefas Comuns de E/S.
Ver também
- Formatos de caminho de ficheiro em sistemas Windows
- E/S de arquivo e fluxo
- Como: Ler texto de um arquivo
- Como: Gravar texto em um arquivo
Aplica-se a
IsPathRooted(ReadOnlySpan<Char>)
- Origem:
- Path.Unix.cs
- Origem:
- Path.Unix.cs
- Origem:
- Path.Unix.cs
- Origem:
- Path.Unix.cs
- Origem:
- Path.Unix.cs
Devolve um valor que indica se o espaço de caracteres especificado que representa um caminho de ficheiro 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 testar.
Devoluções
true se path contiver uma raiz; caso contrário, false.