Path.GetFullPath Méthode

Définition

Surcharges

GetFullPath(String)

Retourne le chemin d'accès absolu de la chaîne de chemin d'accès spécifiée.

GetFullPath(String, String)

Renvoie un chemin d’accès absolu à partir d’un chemin relatif et d’un chemin de base complet.

GetFullPath(String)

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

Retourne le chemin d'accès absolu de la chaîne de chemin d'accès spécifiée.

public:
 static System::String ^ GetFullPath(System::String ^ path);
public static string GetFullPath (string path);
static member GetFullPath : string -> string
Public Shared Function GetFullPath (path As String) As String

Paramètres

path
String

Fichier ou répertoire pour lequel obtenir les informations sur le chemin d’accès absolu.

Retours

Emplacement qualifié complet de path, par exemple « C:\MonFichier.txt ».

Exceptions

path est une chaîne de longueur nulle, contient uniquement des espaces blancs sur les systèmes Windows ou contient un ou plusieurs des caractères non valides définis dans GetInvalidPathChars().

- ou -

Le système n’a pas pu récupérer le chemin d’accès absolu.

L’appelant n’a pas les autorisations requises.

path a la valeur null.

.NET Framework uniquement : path contient un signe deux-points (« : ») qui ne fait pas partie d’un identificateur de volume (par exemple, « c:\ »).

Le chemin et/ou le nom de fichier spécifiés dépassent la longueur maximale définie par le système.

Exemples

L’exemple suivant illustre la GetFullPath méthode sur une plateforme de bureau Windows.

String^ fileName = "myfile.ext";
String^ path = "\\mydir\\";
String^ fullPath;
fullPath = Path::GetFullPath( path );
Console::WriteLine( "GetFullPath('{0}') returns '{1}'", path, fullPath );
fullPath = Path::GetFullPath( fileName );
Console::WriteLine( "GetFullPath('{0}') returns '{1}'", fileName, fullPath );

// Output is based on your current directory, except
// in the last case, where it is based on the root drive
// GetFullPath('mydir') returns 'C:\temp\Demo\mydir'
// GetFullPath('myfile.ext') returns 'C:\temp\Demo\myfile.ext'
// GetFullPath('\mydir') returns 'C:\mydir'
string fileName = "myfile.ext";
string path1 = @"mydir";
string path2 = @"\mydir";
string fullPath;

fullPath = Path.GetFullPath(path1);
Console.WriteLine("GetFullPath('{0}') returns '{1}'",
    path1, fullPath);

fullPath = Path.GetFullPath(fileName);
Console.WriteLine("GetFullPath('{0}') returns '{1}'",
    fileName, fullPath);

fullPath = Path.GetFullPath(path2);
Console.WriteLine("GetFullPath('{0}') returns '{1}'",
    path2, fullPath);

// Output is based on your current directory, except
// in the last case, where it is based on the root drive
// GetFullPath('mydir') returns 'C:\temp\Demo\mydir'
// GetFullPath('myfile.ext') returns 'C:\temp\Demo\myfile.ext'
// GetFullPath('\mydir') returns 'C:\mydir'
Dim fileName As string = "myfile.ext"
Dim path1 As string = "mydir"
Dim path2 As string = "\mydir"
Dim fullPath As string

fullPath = Path.GetFullPath(path1)
Console.WriteLine("GetFullPath('{0}') returns '{1}'", _
    path1, fullPath)

fullPath = Path.GetFullPath(fileName)
Console.WriteLine("GetFullPath('{0}') returns '{1}'", _
    fileName, fullPath)

fullPath = Path.GetFullPath(path2)
Console.WriteLine("GetFullPath('{0}') returns '{1}'", _
    path2, fullPath)

' Output is based on your current directory, except
' in the last case, where it is based on the root drive
' GetFullPath('mydir') returns 'C:\temp\Demo\mydir'
' GetFullPath('myfile.ext') returns 'C:\temp\Demo\myfile.ext'
' GetFullPath('\mydir') returns 'C:\mydir'

Remarques

Le chemin d’accès absolu inclut toutes les informations nécessaires pour localiser un fichier ou un répertoire sur un système.

Le fichier ou le répertoire spécifié par path n’est pas obligatoire pour exister. Par exemple, si c:\temp\newdir est le répertoire actif, l’appel GetFullPath sur un nom de fichier tel que test.txt retourne c:\temp\newdir\test.txt. Le fichier n’a pas besoin d’exister.

Important

S’il s’agit path d’un chemin relatif, cette surcharge retourne un chemin d’accès complet qui peut être basé sur le lecteur actuel et le répertoire actif. Le lecteur actuel et le répertoire actif peuvent changer à tout moment lors de l’exécution d’une application. Par conséquent, le chemin retourné par cette surcharge ne peut pas être déterminé à l’avance. Pour retourner un chemin déterministe, appelez la GetFullPath(String, String) surcharge. Vous pouvez également appeler la IsPathFullyQualified méthode pour déterminer si un chemin d’accès est complet ou relatif et, par conséquent, si un appel à GetFullPath est nécessaire.

Toutefois, s’il path existe, l’appelant doit avoir l’autorisation d’obtenir les informations de chemin d’accès pour path. Notez que contrairement à la plupart des membres de la Path classe, cette méthode accède au système de fichiers.

Cette méthode utilise le répertoire actif et les informations de volume actuel pour qualifier pathentièrement . Si vous spécifiez un nom de fichier uniquement dans path, GetFullPath retourne le chemin complet du répertoire actif.

Si vous transmettez un nom de fichier court, celui-ci est développé vers un nom de fichier long.

Si un chemin d’accès ne contient aucun caractère significatif, il n’est pas valide, sauf s’il contient un ou plusieurs caractères « ». ensuite, il sera analysé en tant que « » ou « ».

.NET Core 1.1 et versions ultérieures et .NET Framework 4.6.2 et versions ultérieures prennent également en charge les chemins qui incluent des noms d’appareils, tels que « \?\?\C:\ ».

Pour plus d’informations sur les formats de chemin d’accès aux fichiers sur Windows, consultez Formats de chemin d’accès aux fichiers sur les systèmes Windows. Pour obtenir la liste des tâches d’E/S courantes, consultez Tâches courantes d’E/S.

Voir aussi

S’applique à

GetFullPath(String, String)

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

Renvoie un chemin d’accès absolu à partir d’un chemin relatif et d’un chemin de base complet.

public:
 static System::String ^ GetFullPath(System::String ^ path, System::String ^ basePath);
public static string GetFullPath (string path, string basePath);
static member GetFullPath : string * string -> string
Public Shared Function GetFullPath (path As String, basePath As String) As String

Paramètres

path
String

Chemin d'accès relatif à concaténer au sein de basePath.

basePath
String

Début d'un chemin d'accès complet.

Retours

Chemin d'accès complet.

Exceptions

path ou basePath est null.

basePath n'est pas un chemin d'accès complet.

- ou -

path ou basePath contient un ou plusieurs caractères non valides définis dans GetInvalidPathChars().

Exemples

L’exemple suivant définit une variable, basePath, pour représenter le répertoire actif d’une application. Il le transmet ensuite à la GetFullPath méthode pour obtenir un chemin complet vers le répertoire de données de l’application.

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string basePath = Environment.CurrentDirectory;
        string relativePath = "./data/output.xml";
 
        // Unexpectedly change the current directory.
        Environment.CurrentDirectory = "C:/Users/Public/Documents/";
        
        string fullPath = Path.GetFullPath(relativePath, basePath);
        Console.WriteLine($"Current directory:\n   {Environment.CurrentDirectory}");
        Console.WriteLine($"Fully qualified path:\n   {fullPath}");
    }
}
// The example displays the following output:
//   Current directory:
//      C:\Users\Public\Documents
//   Fully qualified path:
//      C:\Utilities\data\output.xml
Imports System.IO

Module Program
    Public Sub Main()
        Dim basePath As String = Environment.CurrentDirectory
        Dim relativePath As String = "./data/output.xml"
 
        ' Unexpectedly change the current directory.
        Environment.CurrentDirectory = "C:/Users/Public/Documents/"
        
        Dim fullPath As String = Path.GetFullPath(relativePath, basePath)
        Console.WriteLine($"Current directory:\n   {Environment.CurrentDirectory}")
        Console.WriteLine($"Fully qualified path:\n   {fullPath}")
    End Sub
End Module
' The example displays the following output:
'   Current directory:
'      C:\Users\Public\Documents
'   Fully qualified path:
'      C:\Utilities\data\output.xml

Remarques

Si path est un chemin vide, la méthode retourne basePath. Si path est un chemin complet, la méthode passe path à la GetFullPath(String) méthode et retourne le résultat.

Utilisez cette méthode pour retourner un chemin déterministe basé sur un volume et un répertoire rooté spécifiés lorsque vous utilisez des chemins relatifs. L’utilisation d’un fichier prédéfini basePath plutôt que basé sur le répertoire de lecteur actuel protège contre les chemins de fichiers indésirables causés par des modifications inattendues dans le lecteur et le répertoire actuels.

S’applique à