Path.GetFullPath 方法

定義

多載

GetFullPath(String)

傳回指定路徑字串的絕對路徑。

GetFullPath(String, String)

從相對路徑與完整基底路徑傳回絕對路徑。

GetFullPath(String)

來源:
Path.Unix.cs
來源:
Path.Unix.cs
來源:
Path.Unix.cs

傳回指定路徑字串的絕對路徑。

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

參數

path
String

要為其取得絕對路徑資訊的檔案或目錄。

傳回

path 的完整位置,例如 "C:\MyFile.txt"。

例外狀況

path 是長度為零的字串,只包含 Windows 系統上的空格符,或包含 中 GetInvalidPathChars()定義的一或多個無效字元。

-或-

系統可能不會擷取絕對路徑。

呼叫端沒有必要的權限。

pathnull

僅限 .NET Framework:path包含冒號 (“:”) ,不屬於磁碟區標識符 (的一部分,例如 “c:\”) 。

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

範例

下列範例示範 GetFullPath 以 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'

備註

絕對路徑包含尋找系統上檔案或目錄所需的所有資訊。

path 指定的檔案或目錄不需要存在。 例如,如果 c:\temp\newdir 是目前目錄,請在檔名上呼叫 GetFullPath ,例如 test.txt 傳回c:\temp\newdir\test.txt。 檔案不需要存在。

重要

如果 path 是相對路徑,這個多載會傳回可依據目前磁碟驅動器和目前目錄的完整路徑。 當應用程式執行時,目前磁碟驅動器和目前目錄可以隨時變更。 因此,無法事先判斷此多載傳回的路徑。 若要傳回決定性路徑,請呼叫 GetFullPath(String, String) 多載。 您也可以呼叫 IsPathFullyQualified 方法,以判斷路徑是否為完整或相對路徑,因此是否需要呼叫 GetFullPath

不過,如果 path 存在,呼叫端必須具有取得 的路徑信息 path的許可權。 請注意,不同於 類別的大部分 Path 成員,這個方法會存取文件系統。

這個方法會使用目前的目錄和目前的磁碟區資訊來完整限定 path。 如果您只在 中 path指定檔名, GetFullPath 則會傳回目前目錄的完整路徑。

如果您傳入簡短檔名,則會將其展開為長檔名。

如果路徑不包含任何有效字元,除非路徑包含一或多個 「.」 字元,後面接著任意數目的空格,否則無效;然後將它剖析為 “.” 或 “..”。

.NET Core 1.1 和更新版本和 .NET Framework 4.6.2 和更新版本也支援包含裝置名稱的路徑,例如 “\\?\C:\”。

如需 Windows 上檔案路徑格式的詳細資訊,請參閱 Windows 系統上的檔案路徑格式。 如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

GetFullPath(String, String)

來源:
Path.Unix.cs
來源:
Path.Unix.cs
來源:
Path.Unix.cs

從相對路徑與完整基底路徑傳回絕對路徑。

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

參數

path
String

串連成 basePath 的相對路徑。

basePath
String

完整路徑的開頭。

傳回

絕對路徑。

例外狀況

pathbasePathnull

basePath 不是完整路徑。

-或-

pathbasePath 含有在 GetInvalidPathChars() 中定義的一或多個不正確字元。

範例

下列範例會定義變數 basePath,以代表應用程式的目前目錄。 然後,它會將它傳遞至 GetFullPath 方法,以取得應用程式資料目錄的完整路徑。

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

備註

如果 path 是空路徑,方法會傳 basePath回 。 如果 path 是完整路徑,方法會傳遞 pathGetFullPath(String) 方法並傳回結果。

當您使用相對路徑時,請使用這個方法,根據指定的磁碟區和根目錄傳回決定性路徑。 使用預先定義的 basePath 而非以目前磁碟驅動器目錄為基礎的檔案路徑,可防範目前磁碟驅動器和目錄中發生非預期變更所造成的垃圾檔案路徑。

適用於