Path.IsPathRooted Méthode

Définition

Retourne une valeur qui indique si un chemin d’accès de fichier contient une racine.

Surcharges

Nom Description
IsPathRooted(ReadOnlySpan<Char>)

Retourne une valeur qui indique si l’étendue de caractères spécifiée qui représente un chemin d’accès de fichier contient une racine.

IsPathRooted(String)

Retourne une valeur indiquant si la chaîne de chemin d’accès spécifiée contient une racine.

Remarques

Un chemin d’accès rooté est un chemin d’accès de fichier qui est fixe à un lecteur spécifique ou un chemin UNC ; il contraste avec un chemin d’accès relatif au lecteur actif ou au répertoire de travail. Par exemple, sur les systèmes Windows, un chemin rooté commence par une barre oblique inverse (par exemple, « \Documents ») ou une lettre de lecteur et un signe deux-points (par exemple, « C :Documents »).

Notez que les chemins rootés peuvent être absolus (c’est-à-dire complets) ou relatifs. Un chemin rooté absolu est un chemin complet de la racine d’un lecteur vers un répertoire spécifique. Un chemin rooté relatif spécifie un lecteur, mais son chemin complet est résolu par rapport au répertoire actif. L'exemple suivant illustre la différence.

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

Retourne une valeur qui indique si l’étendue de caractères spécifiée qui représente un chemin d’accès de fichier contient une racine.

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

Paramètres

path
ReadOnlySpan<Char>

Chemin à tester.

Retours

true si path contient une racine ; sinon, false.

Voir aussi

S’applique à

IsPathRooted(String)

Retourne une valeur indiquant si la chaîne de chemin d’accès spécifiée contient une racine.

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

Paramètres

path
String

Chemin à tester.

Retours

true si path contient une racine ; sinon, false.

Exceptions

Versions .NET Framework et .NET Core antérieures à 2.1 : path contient un ou plusieurs des caractères non valides définis dans GetInvalidPathChars().

Exemples

L’exemple suivant montre comment la IsPathRooted méthode peut être utilisée pour tester trois chaînes.

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

Remarques

La IsPathRooted méthode retourne true si le premier caractère est un caractère de séparation de répertoire tel que « \ », ou si le chemin commence par une lettre de lecteur et un signe deux-points (:). Par exemple, elle retourne true des path chaînes telles que « \\MyDir\MyFile.txt», « C :\MyDir » ou « C :MyDir ». Elle retourne false des path chaînes telles que « MyDir ».

Cette méthode ne vérifie pas que le chemin d’accès ou le nom de fichier existe.

Pour obtenir la liste des tâches d’E/S courantes, consultez Tâches d’E/S courantes.

Voir aussi

S’applique à