Path.IsPathRooted Metodo

Definizione

Restituisce un valore che indica se un percorso di file contiene una radice.

Overload

IsPathRooted(ReadOnlySpan<Char>)

Restituisce un valore che indica se l'intervallo di caratteri specificato che rappresenta un percorso di file contiene una radice.

IsPathRooted(String)

Restituisce un valore che indica se la stringa di percorso specificata contiene una radice.

Commenti

Un percorso rooted è il percorso del file fisso a un'unità specifica o a un percorso UNC; contrasta con un percorso relativo all'unità corrente o alla directory di lavoro. Ad esempio, nei sistemi Windows, un percorso rooted inizia con una barra rovesciata (ad esempio "\Documents") o una lettera di unità e due punti (ad esempio, "C:Documents").

Si noti che i percorsi rooted possono essere assoluti (ovvero completi) o relativi. Un percorso rooted assoluto è un percorso completo dalla radice di un'unità a una directory specifica. Un percorso rooted relativo specifica un'unità, ma il relativo percorso completo viene risolto nella directory corrente. Nell'esempio che segue viene illustrata la differenza.

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

Origine:
Path.Unix.cs
Origine:
Path.Unix.cs
Origine:
Path.Unix.cs

Restituisce un valore che indica se l'intervallo di caratteri specificato che rappresenta un percorso di file contiene una radice.

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

Parametri

path
ReadOnlySpan<Char>

Percorso da testare.

Restituisce

true se path contiene un percorso radice, in caso contrario false.

Vedi anche

Si applica a

IsPathRooted(String)

Origine:
Path.Unix.cs
Origine:
Path.Unix.cs
Origine:
Path.Unix.cs

Restituisce un valore che indica se la stringa di percorso specificata contiene una radice.

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

Parametri

path
String

Percorso da testare.

Restituisce

true se path contiene un percorso radice, in caso contrario false.

Eccezioni

.NET Framework e versioni di .NET Core precedenti alla 2.1: path contiene uno o più caratteri non validi definiti in GetInvalidPathChars().

Esempio

Nell'esempio seguente viene illustrato come usare il IsPathRooted metodo per testare tre stringhe.

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

Commenti

Il IsPathRooted metodo restituisce true se il primo carattere è un carattere separatore di directory, ad esempio "\", o se il percorso inizia con una lettera di unità e due punti (:). Ad esempio, restituisce true per path stringhe come "\\MyDir\MyFile.txt", "C:\MyDir" o "C:MyDir". Restituisce false per path stringhe come "MyDir".

Questo metodo non verifica che il percorso o il nome file esista.

Per un elenco delle attività di I/O comuni, vedere Attività di I/O comuni.

Vedi anche

Si applica a