Path.Join 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| 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
- 來源:
- 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、 、 path2、 path3path4 串接起來,並在任何路徑組件間加入目錄分隔符,若尚未存在的話。 若 、 path2或 path3path4 參數的長度path1為零,則方法會串接剩餘的參數。 若結果串接字串的長度為零,則該方法返回 String.Empty。
若 path1 或 path2path3 以不適合目標平台的路徑分隔字元結尾, Join 方法保留原始路徑分隔字元並附加支援的分隔字元。 此問題出現在硬編碼路徑中,若使用 Windows 反斜線(「\」)字元,該字元在 Unix 系統中不被視為路徑分隔符。 若要解決此問題,您可以:
取回屬性的 Path.DirectorySeparatorChar 值,而不是硬編碼目錄分隔字元。
使用斜線(“/”)作為目錄分隔字元。 此字元在 Unix 系統上由 Path.DirectorySeparatorChar 屬性回傳,Windows 系統則由 Path.AltDirectorySeparatorChar 屬性回傳。
與方法 Combine 不同,該 Join 方法不會嘗試根回傳路徑。 (也就是說,若 path2 或 path3path4 或 是絕對路徑, Join 該方法不會像方法 Combine 一樣丟棄先前的路徑。)
並非所有目錄名和檔案名稱的無效字元都被方法解讀為不可接受 Join ,因為你可以用這些字元來搜尋萬用字元。 例如,雖然 Path.Join("c:\\", "temp", "*.txt") 在建立檔案時可能無效,但它作為搜尋字串是有效的。 因此,該 Join 方法成功詮釋了它。
適用於
Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)
- 來源:
- Path.cs
- 來源:
- Path.cs
- 來源:
- 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、 、 path2、 path3path4 串接起來,並在任何路徑組件間加入目錄分隔符,若尚未存在的話。 若 Length 、 path2或 path3path4 參數中任path1一為零,則該方法將剩餘參數串接。 若所有分量中 為 ReadOnlySpan<T>.Length 零,則該方法回傳 String.Empty。
若 path1 或 path2path3 以不適合目標平台的路徑分隔字元結尾, Join 方法保留原始路徑分隔字元並附加支援的分隔字元。 此問題出現在硬編碼路徑中,若使用 Windows 反斜線(「\」)字元,該字元在 Unix 系統中不被視為路徑分隔符。 若要解決此問題,您可以:
取回屬性的 Path.DirectorySeparatorChar 值,而不是硬編碼目錄分隔字元。
使用斜線(“/”)作為目錄分隔字元。 此字元在 Unix 系統上由 Path.DirectorySeparatorChar 屬性回傳,Windows 系統則由 Path.AltDirectorySeparatorChar 屬性回傳。
與方法 Combine 不同,該 Join 方法不會嘗試根回傳路徑。 (也就是說,若 path2 或 path3path4 或 是絕對路徑, Join 該方法不會像方法 Combine 一樣丟棄先前的路徑。)
並非所有目錄名和檔案名稱的無效字元都被方法解讀為不可接受 Join ,因為你可以用這些字元來搜尋萬用字元。 例如,雖然 Path.Join("c:\\", "temp", "*.txt") 在建立檔案時可能無效,但它作為搜尋字串是有效的。 因此,該 Join 方法成功詮釋了它。
適用於
Join(String, String, String)
- 來源:
- Path.cs
- 來源:
- Path.cs
- 來源:
- 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、 、 path2、 path3 串接起來,並在任何路徑組件間加入目錄分隔符,若尚未存在的話。 若 中 path1任一 或 path2path3 參數的長度為零,則方法會串接剩餘的參數。 若結果串接字串的長度為零,則該方法返回 String.Empty。
若 path1 或 path2 以不適合目標平台的路徑分隔字元結尾, Join 方法會保留原始路徑分隔符,並附加支援的分隔符。 此問題出現在硬編碼路徑中,若使用 Windows 反斜線(「\」)字元,該字元在 Unix 系統中不被視為路徑分隔符。 若要解決此問題,您可以:
取回屬性的 Path.DirectorySeparatorChar 值,而不是硬編碼目錄分隔字元。
使用斜線(“/”)作為目錄分隔字元。 此字元在 Unix 系統上由 Path.DirectorySeparatorChar 屬性回傳,Windows 系統則由 Path.AltDirectorySeparatorChar 屬性回傳。
與方法 Combine 不同,該 Join 方法不會嘗試根回傳路徑。 (也就是說,如果 path2 或 path3 是絕對路徑, Join 該方法不會像方法 Combine 一樣丟棄先前的路徑。)
並非所有目錄名和檔案名稱的無效字元都被方法解讀為不可接受 Join ,因為你可以用這些字元來搜尋萬用字元。 例如,雖然 Path.Join("c:\\", "temp", "*.txt") 在建立檔案時可能無效,但它作為搜尋字串是有效的。 因此,該 Join 方法成功詮釋了它。
適用於
Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)
- 來源:
- Path.cs
- 來源:
- Path.cs
- 來源:
- 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.Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>) 方法回傳Path.Combine(String, String, String)路徑的差異。 當第一串是包含磁碟機與根目錄的完全限定路徑,而第二串是第一條路徑的相對路徑時,兩種方法產生的結果是相同的。 在第二次和第三次呼叫該 ShowPathInformation 方法時,兩個方法回傳的字串會產生分歧。 在第二個方法呼叫中,第一個字串參數是磁碟機,第二個則是根目錄。 此 Join 方法將兩串串接並保留重複路徑分隔符。 呼叫該 GetFullPath 方法即可消除重複。 此 Combine 方法會放棄硬碟,並在當前硬碟上回傳一個已根目錄。 如果應用程式目前的磁碟機是 C:\,且該字串用於存取目錄中的檔案,則存取 C: 而非 D:。 最後,由於第三個呼叫 ShowPathInformation 中的最後一個參數是根的 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、 、 path2、 path3 串接起來,並在任何路徑組件間加入目錄分隔符,若尚未存在的話。 若 Length 、 path1或 path2 中任path3一參數為零,則該方法會串接剩餘的參數。 若所有分量中 為 ReadOnlySpan<T>.Length 零,則該方法回傳 String.Empty。
若 path1 或 path2 以不適合目標平台的路徑分隔字元結尾, Join 方法會保留原始路徑分隔符,並附加支援的分隔符。 此問題出現在硬編碼路徑中,若使用 Windows 反斜線(「\」)字元,該字元在 Unix 系統中不被視為路徑分隔符。 若要解決此問題,您可以:
取回屬性的 Path.DirectorySeparatorChar 值,而不是硬編碼目錄分隔字元。
使用斜線(“/”)作為目錄分隔字元。 此字元在 Unix 系統上由 Path.DirectorySeparatorChar 屬性回傳,Windows 系統則由 Path.AltDirectorySeparatorChar 屬性回傳。
與方法 Combine 不同,該 Join 方法不會嘗試根回傳路徑。 (也就是說,如果 path2 或 path3 是絕對路徑, Join 該方法不會像方法 Combine 一樣丟棄先前的路徑。)
並非所有目錄名和檔案名稱的無效字元都被方法解讀為不可接受 Join ,因為你可以用這些字元來搜尋萬用字元。 例如,雖然 Path.Join("c:\\", "temp", "*.txt") 在建立檔案時可能無效,但它作為搜尋字串是有效的。 因此,該 Join 方法成功詮釋了它。
另請參閱
適用於
Join(ReadOnlySpan<String>)
- 來源:
- Path.cs
- 來源:
- Path.cs
- 來源:
- Path.cs
將路徑範圍串連成單一路徑。
public:
static System::String ^ Join(ReadOnlySpan<System::String ^> paths);
public static string Join(scoped ReadOnlySpan<string?> paths);
static member Join : ReadOnlySpan<string> -> string
Public Shared Function Join (paths As ReadOnlySpan(Of String)) As String
參數
- paths
- ReadOnlySpan<String>
一條多條路徑。
傳回
連結的路徑。
適用於
Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>)
- 來源:
- Path.cs
- 來源:
- Path.cs
- 來源:
- 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.Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>) 方法回傳Path.Combine(String, String)路徑的差異。 當第一串是包含磁碟機與根目錄的完整限定路徑,而第二串是第一條路徑的相對路徑時,兩種方法產生的結果相同。 在第二次和第三次呼叫該 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/'
備註
此方法簡單地將 和 path 串接path2起來,並在兩個路徑組件之間加入目錄分隔符,若在 的path1結尾或起始path2處尚未存在該分隔符。 若 或 為 Lengthpath1path2 零,則方法回傳另一條路徑。 若 Length 與 皆path1path2為零,則該方法回傳 String.Empty。
若 path1 以不適合目標平台 Join 的路徑分隔字元結尾,方法會保留原始路徑分隔字元並附加支援的分隔字元。 此問題出現在硬編碼路徑中,若使用 Windows 反斜線(「\」)字元,該字元在 Unix 系統中不被視為路徑分隔符。 若要解決此問題,您可以:
取回屬性的 Path.DirectorySeparatorChar 值,而不是硬編碼目錄分隔字元。
使用斜線(“/”)作為目錄分隔字元。 此字元在 Unix 系統上由 Path.DirectorySeparatorChar 屬性回傳,Windows 系統則由 Path.AltDirectorySeparatorChar 屬性回傳。
與方法 Combine 不同,該 Join 方法不會嘗試根回傳路徑。 (也就是說,若 path2 是絕對路徑,Join該方法不會像方法path1那樣丟棄path2並回傳Combine。)以下範例說明兩種方法回傳路徑的差異。 如果 的 path2 來源是使用者輸入,該 Combine 方法使使用者能夠存取應用程式原本不打算讓存取的檔案系統資源(例如 C:/Users/User1/Documents/Financial/ )。
並非所有目錄名和檔案名稱的無效字元都被方法解讀為不可接受 Join ,因為你可以用這些字元來搜尋萬用字元。 例如,雖然 Path.Join("c:\\", "*.txt") 在建立檔案時可能無效,但它作為搜尋字串是有效的。 因此,該 Join 方法成功詮釋了它。
另請參閱
適用於
Join(String[])
- 來源:
- Path.cs
- 來源:
- Path.cs
- 來源:
- 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 並在路徑組件間加入目錄分隔字元(若尚未存在的話)。 若 中paths任一路徑的 為Length零,則該方法會串接剩餘的參數。 若串接字串長度為零,則該方法回傳 String.Empty。
若 中 paths除最後一條外的任何路徑以不適合目標平台的路徑分隔字元結尾, Join 方法保留原始路徑分隔符字元並附加支援的分隔符字元。 此問題出現在硬編碼路徑中,若使用 Windows 反斜線(「\」)字元,該字元在 Unix 系統中不被視為路徑分隔符。 若要解決此問題,您可以:
取回屬性的 Path.DirectorySeparatorChar 值,而不是硬編碼目錄分隔字元。
使用斜線(“/”)作為目錄分隔字元。 此字元在 Unix 系統上由 Path.DirectorySeparatorChar 屬性回傳,Windows 系統則由 Path.AltDirectorySeparatorChar 屬性回傳。
與方法 Combine 不同,該 Join 方法不會嘗試根回傳路徑。 (也就是說,如果 中 paths除第一條外的任何路徑都是絕對路徑,該 Join 方法不會像方法 Combine 一樣丟棄先前的路徑。)
並非所有目錄名和檔案名稱的無效字元都被方法解讀為不可接受 Join ,因為你可以用這些字元來搜尋萬用字元。 例如,雖然 Path.Join("c:\\", "temp", "*.txt") 在建立檔案時可能無效,但它作為搜尋字串是有效的。 因此,該 Join 方法成功詮釋了它。
適用於
Join(String, String)
- 來源:
- Path.cs
- 來源:
- Path.cs
- 來源:
- 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 連接,並在任何路徑元件間加入目錄分隔符,若尚未存在的話。 若任一 path1 或 path2 的長度為零,則該方法會串接剩餘的參數。 若結果串接字串的長度為零,則該方法返回 String.Empty。
若 path1 以不適合目標平台 Join 的路徑分隔字元結尾,方法會保留原始路徑分隔字元並附加支援的分隔字元。 此問題出現在硬編碼路徑中,若使用 Windows 反斜線(「\」)字元,該字元在 Unix 系統中不被視為路徑分隔符。 若要解決此問題,您可以:
取回屬性的 Path.DirectorySeparatorChar 值,而不是硬編碼目錄分隔字元。
使用斜線(“/”)作為目錄分隔字元。 此字元在 Unix 系統上由 Path.DirectorySeparatorChar 屬性回傳,Windows 系統則由 Path.AltDirectorySeparatorChar 屬性回傳。
與方法 Combine 不同,該 Join 方法不會嘗試根回傳路徑。 (也就是說,如果 path2 是絕對路徑, Join 該方法不會像方法 Combine 那樣丟棄先前的路徑。)
並非所有目錄名和檔案名稱的無效字元都被方法解讀為不可接受 Join ,因為你可以用這些字元來搜尋萬用字元。 例如,雖然 Path.Join("c:\\", "temp", "*.txt") 在建立檔案時可能無效,但它作為搜尋字串是有效的。 因此,該 Join 方法成功詮釋了它。