Path.IsPathRooted Méthode

Définition

Renvoie une valeur qui indique si un chemin d'accès contient une racine.

Surcharges

IsPathRooted(ReadOnlySpan<Char>)

Renvoie une valeur indiquant si l'étendue de caractères spécifiée qui représente un chemin de fichier contient une racine.

IsPathRooted(String)

Renvoie 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 sur un lecteur ou un chemin UNC spécifique ; il contraste avec un chemin d’accès relatif au lecteur ou au répertoire de travail actuel. Par exemple, sur les systèmes Windows, un chemin d’accès racine commence par une barre oblique inverse (par exemple, « \Documents ») ou une lettre de lecteur et deux-points (par exemple, « C:Documents »).

Notez que les chemins racines peuvent être absolus (c’est-à-dire complets) ou relatifs. Un chemin d’accès rooté absolu est un chemin complet de la racine d’un lecteur vers un répertoire spécifique. Un chemin d’accès racine 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>)

Source:
Path.Unix.cs
Source:
Path.Unix.cs
Source:
Path.Unix.cs

Renvoie une valeur indiquant si l'étendue de caractères spécifiée qui représente un chemin 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 d’accès à tester.

Retours

true si path contient une racine ; sinon, false.

Voir aussi

S’applique à

IsPathRooted(String)

Source:
Path.Unix.cs
Source:
Path.Unix.cs
Source:
Path.Unix.cs

Renvoie 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);
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 d’accès à tester.

Retours

true si path contient une racine ; sinon, false.

Exceptions

.NET Framework et .NET Core versions antérieures à 2.1 : path contient un ou plusieurs 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.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

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 deux-points (:). Par exemple, il retourne true pour path des chaînes telles que « \\MyDir\MyFile.txt », « C:\MyDir » ou « C:MyDir ». Il retourne false pour path les 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 courantes d’E/S.

Voir aussi

S’applique à