共用方式為


Path.Join 方法

定義

多載

Join(String, String, String, String)

將四個路徑串連成單一路徑。

Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

將四個路徑元件串連成單一路徑。

Join(String, String, String)

將三個路徑串連成單一路徑。

Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

將三個路徑元件串連成單一路徑。

Join(ReadOnlySpan<String>)
Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

將兩個路徑元件串連成單一路徑。

Join(String[])

將路徑陣列串連成單一路徑。

Join(String, String)

將兩個路徑串連成單一路徑。

Join(String, String, String, String)

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

將四個路徑串連成單一路徑。

public:
 static System::String ^ Join(System::String ^ path1, System::String ^ path2, System::String ^ path3, System::String ^ path4);
public static string Join (string? path1, string? path2, string? path3, string? path4);
static member Join : string * string * string * string -> string
Public Shared Function Join (path1 As String, path2 As String, path3 As String, path4 As String) As String

參數

path1
String

第一個聯結路徑。

path2
String

第二個聯結路徑。

path3
String

第三個聯結路徑。

path4
String

第四個聯結路徑。

傳回

串連的路徑。

備註

如果沒有任何路徑元件,這個方法只會串path連 、 path2path3path4 ,並在任何路徑元件之間新增目錄分隔符。 如果、 path2path3path4 自變數的任何path1長度為零,則方法會串連其餘自變數。 如果產生的串連字串長度為零,方法會傳 String.Empty回 。

如果 path1path2path3 結尾為不適用於目標平臺的路徑分隔符字元,則 Join 方法會保留原始路徑分隔符,並附加支援的分隔符。 此問題發生在使用 Windows 反斜杠 (“\”) 字元的硬式編碼路徑中,這在 Unix 系統上無法辨識為路徑分隔符。 若要解決此問題,您可以:

Combine不同於 方法,Join方法不會嘗試對傳回的路徑進行根目錄。 (也就是說,如果 path2path3path4 是絕對路徑,方法 Join 就不會捨棄先前的路徑,因為方法會像 方法一樣 Combine 。)

並非所有目錄和檔名無效的字元都會被 Join 方法解譯為無法接受,因為您可以使用這些字元來搜尋通配符。 例如,雖然 Path.Join("c:\\", "temp", "*.txt") 在建立檔案時可能無效,但它有效做為搜尋字串。 因此,方法 Join 會成功解譯它。

適用於

Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

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

將四個路徑元件串連成單一路徑。

public:
 static System::String ^ Join(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, ReadOnlySpan<char> path3, ReadOnlySpan<char> path4);
public static string Join (ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, ReadOnlySpan<char> path3, ReadOnlySpan<char> path4);
static member Join : ReadOnlySpan<char> * ReadOnlySpan<char> * ReadOnlySpan<char> * ReadOnlySpan<char> -> string
Public Shared Function Join (path1 As ReadOnlySpan(Of Char), path2 As ReadOnlySpan(Of Char), path3 As ReadOnlySpan(Of Char), path4 As ReadOnlySpan(Of Char)) As String

參數

path1
ReadOnlySpan<Char>

包含第一個聯結路徑的字元範圍。

path2
ReadOnlySpan<Char>

包含第二個聯結路徑的字元範圍。

path3
ReadOnlySpan<Char>

包含第三個聯結路徑的字元範圍。

path4
ReadOnlySpan<Char>

包含第四個聯結路徑的字元範圍。

傳回

串連的路徑。

備註

如果沒有任何路徑元件,這個方法只會串path連 、 path2path3path4 ,並在任何路徑元件之間新增目錄分隔符。 Length如果、 path2path3path4 自變數的 path1為零,則方法會串連其餘的自變數。 ReadOnlySpan<T>.Length如果所有元件的 為零,方法會傳String.Empty回 。

如果 path1path2path3 結尾為不適用於目標平臺的路徑分隔符字元,則 Join 方法會保留原始路徑分隔符,並附加支援的分隔符。 此問題發生在使用 Windows 反斜杠 (“\”) 字元的硬式編碼路徑中,這在 Unix 系統上無法辨識為路徑分隔符。 若要解決此問題,您可以:

Combine不同於 方法,Join方法不會嘗試對傳回的路徑進行根目錄。 (也就是說,如果 path2path3path4 是絕對路徑,方法 Join 就不會捨棄先前的路徑,因為方法會像 方法一樣 Combine 。)

並非所有目錄和檔名無效的字元都會被 Join 方法解譯為無法接受,因為您可以使用這些字元來搜尋通配符。 例如,雖然 Path.Join("c:\\", "temp", "*.txt") 在建立檔案時可能無效,但它有效做為搜尋字串。 因此,方法 Join 會成功解譯它。

適用於

Join(String, String, String)

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

將三個路徑串連成單一路徑。

public:
 static System::String ^ Join(System::String ^ path1, System::String ^ path2, System::String ^ path3);
public static string Join (string? path1, string? path2, string? path3);
static member Join : string * string * string -> string
Public Shared Function Join (path1 As String, path2 As String, path3 As String) As String

參數

path1
String

第一個聯結路徑。

path2
String

第二個聯結路徑。

path3
String

第三個聯結路徑。

傳回

串連的路徑。

備註

如果沒有任何路徑元件,這個方法只會串 path連 、 path2path3 ,並在任何路徑元件之間新增目錄分隔符。 如果的任何 path1path2path3 自變數的長度為零,則方法會串連其餘的自變數。 如果產生的串連字串長度為零,方法會傳 String.Empty回 。

如果 path1path2 結尾為不適合目標平臺的路徑分隔符,方法 Join 會保留原始路徑分隔符,並附加支援的分隔符。 此問題發生在使用 Windows 反斜杠 (“\”) 字元的硬式編碼路徑中,這在 Unix 系統上無法辨識為路徑分隔符。 若要解決此問題,您可以:

Combine不同於 方法,Join方法不會嘗試對傳回的路徑進行根目錄。 (也就是說,如果 path2path3 是絕對路徑,方法 Join 就不會捨棄先前的路徑,因為 Combine 方法會是 。)

並非所有目錄和檔名無效的字元都會被 Join 方法解譯為無法接受,因為您可以使用這些字元來搜尋通配符。 例如,雖然 Path.Join("c:\\", "temp", "*.txt") 在建立檔案時可能無效,但它有效做為搜尋字串。 因此,方法 Join 會成功解譯它。

適用於

Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

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

將三個路徑元件串連成單一路徑。

public:
 static System::String ^ Join(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, ReadOnlySpan<char> path3);
public static string Join (ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, ReadOnlySpan<char> path3);
static member Join : ReadOnlySpan<char> * ReadOnlySpan<char> * ReadOnlySpan<char> -> string
Public Shared Function Join (path1 As ReadOnlySpan(Of Char), path2 As ReadOnlySpan(Of Char), path3 As ReadOnlySpan(Of Char)) As String

參數

path1
ReadOnlySpan<Char>

包含第一個聯結路徑的字元範圍。

path2
ReadOnlySpan<Char>

包含第二個聯結路徑的字元範圍。

path3
ReadOnlySpan<Char>

包含第三個聯結路徑的字元範圍。

傳回

串連的路徑。

範例

下列範例說明 和 Path.Combine(String, String, String) 方法所Path.Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)傳回路徑的差異。 當第一個字串是包含磁碟驅動器和根目錄的完整路徑,而第二個是來自第一個路徑的相對路徑時,這兩種方法會產生相同的結果。 在方法的第二和第三個呼叫 ShowPathInformation 中,兩個方法所傳回的字串會分位。 在第二個方法呼叫中,第一個字元串自變數是磁碟驅動器,而第二個是根目錄。 方法 Join 會串連兩個字串,並保留重複的路徑分隔符。 對方法的 GetFullPath 呼叫會消除重複。 方法 Combine 會放棄磁碟驅動器,並傳回目前磁碟驅動器上的根目錄。 如果應用程式的目前磁碟驅動器是 C:\,而且字串是用來存取目錄中的檔案或檔案,則會存取 C: 而不是 D:。 最後,因為第三次呼叫 ShowPathInformation 中的最後一個自變數是rooted, Join 所以方法只會將它附加至前兩個自變數,以建立不區分的檔案路徑,而 Combine 方法會捨棄前兩個字元串並傳回第三個字符串。 使用此字串進行檔案存取可能會讓應用程式對敏感性檔案進行非預期的存取。

using System;
using System.IO;

class Program3
{
    static void Main()
    {
        ShowPathInformation("C:/", "users/user1/documents", "letters");
        ShowPathInformation("D:/", "/users/user1/documents", "letters");
        ShowPathInformation("D:/", "users/user1/documents", "C:/users/user1/documents/data");
    }

   private static void ShowPathInformation(string path1, string path2, string path3)
    {
        Console.WriteLine($"Concatenating  '{path1}', '{path2}', and '{path3}'");
        Console.WriteLine($"   Path.Join:     '{Path.Join(path1, path2, path3)}'");
        Console.WriteLine($"   Path.Combine:  '{Path.Combine(path1, path2, path3)}'");
        Console.WriteLine($"   {Path.GetFullPath(Path.Join(path1, path2, path3))}");
    }
}
// The example displays the following output if run on a Windows system:
// Concatenating  'C:/', 'users/user1/documents', and 'letters'
//    Path.Join:     'C:/users/user1/documents\letters'
//    Path.Combine:  'C:/users/user1/documents\letters'
//    C:\users\user1\documents\letters
// Concatenating  'D:/', '/users/user1/documents', and 'letters'
//    Path.Join:     'D://users/user1/documents\letters'
//    Path.Combine:  '/users/user1/documents\letters'
//    D:\users\user1\documents\letters
// Concatenating  'D:/', 'users/user1/documents', and 'C:/users/user1/documents/data'
//    Path.Join:     'D:/users/user1/documents\C:/users/user1/documents/data'
//    Path.Combine:  'C:/users/user1/documents/data'
//    D:\users\user1\documents\C:\users\user1\documents\data
Imports System.IO

Module Program
    Public Sub Main()
        Dim path1 As String = "C:/"
        Dim path2 As String = "users/user1/documents"
        Dim path3 As String = "letters"
        ShowPathInformation(path1, path2, path3)
        
        path1 = "D:/"
        path2 =  "/users/user1/documents"
        path3 = "letters"
        ShowPathInformation(path1, path2, path3)

        path1 = "D:/"
        path2 =  "users/user1/documents"
        path3 = "C:/users/user1/documents/data"
        ShowPathInformation(path1, path2, path3)
    End Sub

   Private Sub ShowPathInformation(path1 As String, path2 As String, path3 As String)
        Dim result = Path.Join(path1.AsSpan(), path2.AsSpan(), path3.AsSpan())
        Console.WriteLine($"Concatenating  '{path1}, '{path2}', and `{path3}'")
        Console.WriteLine($"   Path.Join:     '{result}'")
        Console.WriteLine($"   Path.Combine:  '{Path.Combine(path1, path2, path3)}'")
    End Sub
End Module
' The example displays the following output if run on a Windows system:
'   Concatenating  'C:/, 'users/user1/documents', and `letters'
'      Path.Join:     'C:/users/user1/documents\letters'
'      Path.Combine:  'C:/users/user1/documents\letters'
'
'   Concatenating  'D:/, '/users/user1/documents', and `letters'
'      Path.Join:     'D:'users/user1/documents\letters'
'      Path.Combine:  '/users/user1/documents\letters'
'
'   Concatenating  'D:/, 'users/user1/documents', and `C:/users/user1/documents/data'
'      Path.Join:     'D:/users/user1/documents\C:/users/user1/documents/data'
'      Path.Combine:  'C:/users/user1/documents/data'

備註

如果沒有任何路徑元件,這個方法只會串 path連 、 path2path3 ,並在任何路徑元件之間新增目錄分隔符。 Length如果、 path2path3 自變數的 path1為零,則方法會串連其餘的自變數。 ReadOnlySpan<T>.Length如果所有元件的 為零,方法會傳String.Empty回 。

如果 path1path2 結尾為不適合目標平臺的路徑分隔符,方法 Join 會保留原始路徑分隔符,並附加支援的分隔符。 此問題發生在使用 Windows 反斜杠 (“\”) 字元的硬式編碼路徑中,這在 Unix 系統上無法辨識為路徑分隔符。 若要解決此問題,您可以:

Combine不同於 方法,Join方法不會嘗試對傳回的路徑進行根目錄。 (也就是說,如果 path2path3 是絕對路徑,方法 Join 就不會捨棄先前的路徑,因為 Combine 方法會是 。)

並非所有目錄和檔名無效的字元都會被 Join 方法解譯為無法接受,因為您可以使用這些字元來搜尋通配符。 例如,雖然 Path.Join("c:\\", "temp", "*.txt") 在建立檔案時可能無效,但它有效做為搜尋字串。 因此,方法 Join 會成功解譯它。

另請參閱

適用於

Join(ReadOnlySpan<String>)

public:
 static System::String ^ Join(ReadOnlySpan<System::String ^> paths);
public static string Join (ReadOnlySpan<string?> paths);
static member Join : ReadOnlySpan<string> -> string
Public Shared Function Join (paths As ReadOnlySpan(Of String)) As String

參數

傳回

適用於

Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

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

將兩個路徑元件串連成單一路徑。

public:
 static System::String ^ Join(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2);
public static string Join (ReadOnlySpan<char> path1, ReadOnlySpan<char> path2);
static member Join : ReadOnlySpan<char> * ReadOnlySpan<char> -> string
Public Shared Function Join (path1 As ReadOnlySpan(Of Char), path2 As ReadOnlySpan(Of Char)) As String

參數

path1
ReadOnlySpan<Char>

包含第一個聯結路徑的字元範圍。

path2
ReadOnlySpan<Char>

包含第二個聯結路徑的字元範圍。

傳回

合併的路徑。

範例

下列範例說明 和 Path.Combine(String, String) 方法所Path.Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>)傳回路徑的差異。 當第一個字串是包含磁碟驅動器和根目錄的完整路徑,而第二個字元串是來自第一個路徑的相對路徑時,這兩種方法會產生相同的結果。 在方法的第二和第三個呼叫 ShowPathInformation 中,兩個方法所傳回的字串會分位。 在第二個方法呼叫中,第一個字元串自變數是磁碟驅動器,而第二個是根目錄。 方法 Join 會串連兩個字串,並保留重複的路徑分隔符。 方法 Combine 會放棄磁碟驅動器,並傳回目前磁碟驅動器上的根目錄。 如果應用程式的目前磁碟驅動器是 C:\,而且字串是用來存取目錄中的檔案或檔案,則會存取 C: 而不是 D:。 最後,因為第三次呼叫 ShowPathInformation 中的兩個自變數都是根目錄, Join 所以方法只會附加它們以建立不區分的檔案路徑,而 Combine 方法則會捨棄第一個字元串並傳回第二個字符串。 使用此字串進行檔案存取可能會讓應用程式對敏感性檔案進行非預期的存取。

using System;
using System.IO;

class Program2
{
    static void Main()
    {
        var path1 = "C:/Program Files/";
        var path2 = "Utilities/SystemUtilities";
        ShowPathInformation(path1, path2);

        path1 = "C:/";
        path2 = "/Program Files";
        ShowPathInformation(path1, path2);

        path1 = "C:/Users/Public/Documents/";
        path2 = "C:/Users/User1/Documents/Financial/";
        ShowPathInformation(path1, path2);
    }

    private static void ShowPathInformation(string path1, string path2)
    {
        var result = Path.Join(path1.AsSpan(), path2.AsSpan());
        Console.WriteLine($"Concatenating  '{path1}' and '{path2}'");
        Console.WriteLine($"   Path.Join:     '{result}'");
        Console.WriteLine($"   Path.Combine:  '{Path.Combine(path1, path2)}'");
    }
}
// The example displays the following output if run on a Windows system:
//    Concatenating  'C:/Program Files/' and 'Utilities/SystemUtilities'
//       Path.Join:     'C:/Program Files/Utilities/SystemUtilities'
//       Path.Combine:  'C:/Program Files/Utilities/SystemUtilities'
//
//    Concatenating  'C:/' and '/Program Files'
//       Path.Join:     'C://Program Files'
//       Path.Combine:  '/Program Files'
//
//    Concatenating  'C:/Users/Public/Documents/' and 'C:/Users/User1/Documents/Financial/'
//       Path.Join:     'C:/Users/Public/Documents/C:/Users/User1/Documents/Financial/'
//       Path.Combine:  'C:/Users/User1/Documents/Financial/'
Imports System.IO

Module Example
    Public Sub Main()
        Dim path1 = "C:/Program Files/"
        Dim path2 = "Utilities/SystemUtilities"
        ShowPathInformation(path1, path2)

        path1 = "C:/"
        path2 = "/Program Files"
        ShowPathInformation(path1, path2)

        path1 = "C:/Users/Public/Documents/"
        path2 = "C:/Users/User1/Documents/Financial/"
        ShowPathInformation(path1, path2)
    End Sub

    Private Sub ShowPathInformation(path1 As String, path2 As String)
        Dim result = Path.Join(path1.AsSpan(), path2.AsSpan())
        Console.WriteLine($"Concatenating  '{path1}' and '{path2}'")
        Console.WriteLine($"   Path.Join:     '{result}'")
        Console.WriteLine($"   Path.Combine:  '{Path.Combine(path1, path2)}'")
        Console.WriteLine()
    End Sub
End Module
' The example displays the following output if run on a Windows system:
'    Concatenating  'C:/Program Files/' and 'Utilities/SystemUtilities'
'       Path.Join:     'C:/Program Files/Utilities/SystemUtilities'
'       Path.Combine:  'C:/Program Files/Utilities/SystemUtilities'
'
'    Concatenating  'C:/' and '/Program Files'
'       Path.Join:     'C:'Program Files'
'       Path.Combine:  '/Program Files'
'
'    Concatenating  'C:/Users/Public/Documents/' and 'C:/Users/User1/Documents/Financial/'
'       Path.Join:     'C:/Users/Public/Documents/C:/Users/User1/Documents/Financial/'
'       Path.Combine:  'C:/Users/User1/Documents/Financial/'

備註

如果兩個路徑元件尚未存在path1或 開頭path2,這個方法只會串path連 和 path2 ,並在兩個路徑元件之間新增目錄分隔符。 Length如果 或 path2path1 為零,方法會傳回另一個路徑。 Length如果與 path2path1 為零,方法會傳String.Empty回 。

如果 path1 結尾為不適合目標平臺的路徑分隔符, Join 則方法會保留原始路徑分隔符,並附加支援的路徑分隔符。 此問題發生在使用 Windows 反斜杠 (“\”) 字元的硬式編碼路徑中,這在 Unix 系統上無法辨識為路徑分隔符。 若要解決此問題,您可以:

Combine不同於 方法,Join方法不會嘗試對傳回的路徑進行根目錄。 (也就是說,如果 path2 是絕對路徑,則 Join 方法不會捨棄 path1 並傳回 path2 ,因為 Combine 方法 does.) 下列範例說明兩種方法所傳回路徑的差異。 如果 的來源 path2 是使用者輸入, Combine 則 方法可讓使用者存取檔系統資源, (例如 C:/Users/User1/Documents/Financial/ ,在範例中) 應用程式不想要存取。

並非所有目錄和檔名無效的字元都會被 Join 方法解譯為無法接受,因為您可以使用這些字元來搜尋通配符。 例如,雖然 Path.Join("c:\\", "*.txt") 在建立檔案時可能無效,但它有效做為搜尋字串。 因此,方法 Join 會成功解譯它。

另請參閱

適用於

Join(String[])

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

將路徑陣列串連成單一路徑。

public:
 static System::String ^ Join(... cli::array <System::String ^> ^ paths);
public static string Join (params string?[] paths);
static member Join : string[] -> string
Public Shared Function Join (ParamArray paths As String()) As String

參數

paths
String[]

路徑陣列。

傳回

串連的路徑。

備註

這個方法只會串連 中的所有 paths 字串,並在其中一個路徑元件之間新增目錄分隔符,如果其中一個路徑元件不存在的話。 Length如果 中paths任何路徑的 為零,則方法會串連其餘自變數。 如果產生的串連字串長度為零,方法會傳 String.Empty回 。

如果 中 paths有任何路徑,但最後一個路徑除外,結束於不適合目標平臺的路徑分隔符,方法 Join 會保留原始路徑分隔符並附加支援的分隔符。 此問題發生在使用 Windows 反斜杠 (“\”) 字元的硬式編碼路徑中,這在 Unix 系統上無法辨識為路徑分隔符。 若要解決此問題,您可以:

Combine不同於 方法,Join方法不會嘗試對傳回的路徑進行根目錄。 (也就是說,如果 中的任一路徑 paths除外,除了第一個路徑之外,就是絕對路徑,則 Join 方法不會捨棄先前的路徑,因為方法會像方法一樣 Combine 捨棄上一個路徑。)

並非所有目錄和檔名無效的字元都會被 Join 方法解譯為無法接受,因為您可以使用這些字元來搜尋通配符。 例如,雖然 Path.Join("c:\\", "temp", "*.txt") 在建立檔案時可能無效,但它有效做為搜尋字串。 因此,方法 Join 會成功解譯它。

適用於

Join(String, String)

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

將兩個路徑串連成單一路徑。

public:
 static System::String ^ Join(System::String ^ path1, System::String ^ path2);
public static string Join (string? path1, string? path2);
static member Join : string * string -> string
Public Shared Function Join (path1 As String, path2 As String) As String

參數

path1
String

第一個聯結路徑。

path2
String

第二個聯結路徑。

傳回

串連的路徑。

備註

如果沒有任何路徑元件,這個方法只會串 path 連 和 path2 ,並在任何路徑元件之間新增目錄分隔符。 如果 或 path2path1長度為零,方法會串連其餘自變數。 如果產生的串連字串長度為零,方法會傳 String.Empty回 。

如果 path1 結尾為不適合目標平臺的路徑分隔符, Join 則方法會保留原始路徑分隔符,並附加支援的路徑分隔符。 此問題發生在使用 Windows 反斜杠 (“\”) 字元的硬式編碼路徑中,這在 Unix 系統上無法辨識為路徑分隔符。 若要解決此問題,您可以:

Combine不同於 方法,Join方法不會嘗試對傳回的路徑進行根目錄。 (也就是說,如果 path2 是絕對路徑,方法 Join 就不會捨棄先前的路徑,因為 Combine 方法會是 。)

並非所有目錄和檔名無效的字元都會被 Join 方法解譯為無法接受,因為您可以使用這些字元來搜尋通配符。 例如,雖然 Path.Join("c:\\", "temp", "*.txt") 在建立檔案時可能無效,但它有效做為搜尋字串。 因此,方法 Join 會成功解譯它。

適用於