Path.GetFullPath Metodo

Definizione

Overload

GetFullPath(String)

Restituisce il percorso assoluto della stringa di percorso specificata.

GetFullPath(String, String)

Restituisce un percorso assoluto da un percorso relativo e un percorso di base completo.

GetFullPath(String)

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

Restituisce il percorso assoluto della stringa di percorso specificata.

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

Parametri

path
String

File o directory per cui ottenere informazioni relative al percorso assoluto.

Restituisce

Percorso completo di path, ad esempio "C:\MyFile.txt".

Eccezioni

path è una stringa di lunghezza zero, contiene solo spazi vuoti nei sistemi Windows oppure contiene uno o più caratteri non validi definiti in GetInvalidPathChars().

-oppure-

Il sistema non è riuscito a recuperare il percorso assoluto.

Il chiamante non ha le autorizzazioni richieste.

path è null.

Solo .NET Framework: path contiene due punti (":") che non fa parte di un identificatore di volume (ad esempio, "c:\").

Il percorso specificato, il nome file o entrambi superano la lunghezza massima definita dal sistema.

Esempio

Nell'esempio seguente viene illustrato il GetFullPath metodo in una piattaforma desktop basata su 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'

Commenti

Il percorso assoluto include tutte le informazioni necessarie per individuare un file o una directory in un sistema.

Il file o la directory specificata da path non è necessario esistere. Ad esempio, se c:\temp\newdir è la directory corrente, chiamare GetFullPath su un nome di file, ad esempio test. txt restituisce c:\temp\newdir\test.txt. Il file non deve esistere.

Importante

Se path è un percorso relativo, questo overload restituisce un percorso completo che può essere basato sull'unità corrente e sulla directory corrente. L'unità corrente e la directory corrente possono cambiare in qualsiasi momento durante l'esecuzione di un'applicazione. Di conseguenza, il percorso restituito da questo overload non può essere determinato in anticipo. Per restituire un percorso deterministico, chiamare l'overload GetFullPath(String, String) . È anche possibile chiamare il IsPathFullyQualified metodo per determinare se un percorso è completo o relativo e quindi se è necessaria una chiamata a GetFullPath .

Tuttavia, se path esiste, il chiamante deve disporre dell'autorizzazione per ottenere le informazioni sul percorso per path. Si noti che, a differenza della maggior parte dei membri della Path classe, questo metodo accede al file system.

Questo metodo usa la directory corrente e le informazioni sul volume corrente per qualificare pathcompletamente . Se si specifica un nome di file solo in path, GetFullPath restituisce il percorso completo della directory corrente.

Se si passa un nome di file breve, viene espanso in un nome di file lungo.

Se un percorso non contiene caratteri significativi, non è valido a meno che non contenga uno o più caratteri "." seguiti da un numero qualsiasi di spazi; verrà quindi analizzato come "." o "..".

.NET Core 1.1 e versioni successive e .NET Framework 4.6.2 e versioni successive supportano anche percorsi che includono i nomi dei dispositivi, ad esempio "\\?\C:\".

Per altre informazioni sui formati di percorso dei file in Windows, vedere Formati di percorso dei file nei sistemi Windows. Per un elenco delle attività di I/O comuni, vedere Attività di I/O comuni.

Vedi anche

Si applica a

GetFullPath(String, String)

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

Restituisce un percorso assoluto da un percorso relativo e un percorso di base completo.

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

Parametri

path
String

Percorso relativo da concatenare a basePath.

basePath
String

Inizio di un percorso completo.

Restituisce

Percorso assoluto.

Eccezioni

path o basePath è null.

basePath non è un percorso completo.

-oppure-

path o basePath contiene uno o più caratteri non validi definiti in GetInvalidPathChars().

Esempio

L'esempio seguente definisce una variabile, basePath, per rappresentare la directory corrente di un'applicazione. Viene quindi passato al GetFullPath metodo per ottenere un percorso completo alla directory dei dati dell'applicazione.

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

Commenti

Se path è un percorso vuoto, il metodo restituisce basePath. Se path è un percorso completo, il metodo passa path al GetFullPath(String) metodo e restituisce il risultato.

Utilizzare questo metodo per restituire un percorso deterministico basato su un volume e una directory rooted specificati quando si usano percorsi relativi. L'uso di un oggetto predefinito basePath anziché basato sulla directory dell'unità corrente protegge da percorsi di file indesiderati causati da modifiche impreviste nell'unità e nella directory correnti.

Si applica a