Path.GetFullPath 方法

定義

多載

名稱 Description
GetFullPath(String, String)

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

GetFullPath(String)

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

GetFullPath(String, String)

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

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 是完全限定路徑,該方法會傳遞 path 給該 GetFullPath(String) 方法並回傳結果。

當你使用相對路徑時,可以用這個方法回傳一個基於指定磁碟區和根目錄的確定性路徑。 使用預先定義 basePath 的檔案,而非基於目前磁碟目錄的目錄,可以避免因當前磁碟和目錄的意外變更而產生的不想要的檔案路徑。

適用於

GetFullPath(String)

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

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

僅限 Framework .NET:path 包含一個不屬於卷識別碼的冒號(“:”)(例如「c:\」)。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

範例

以下範例示範了在基於Windows的桌面平台上的 GetFullPath 方法。

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 是目前目錄,呼叫 GetFullPathtest.txt 這樣的檔名會回傳 c:\temp\newdir\test.txt。 檔案不必存在。

Important

path 為相對路徑,此過載會回傳一個完全限定的路徑,可根據目前硬碟與目錄來判斷。 當前磁碟和目錄可隨時在應用程式執行時更換。 因此,過載回傳的路徑無法事先確定。 若要返回確定性路徑,呼叫 overload 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 任務的清單,請參見 Common I/O 任務

另請參閱

適用於