Path.Combine 方法

定義

將字串合併成路徑。

多載

Combine(ReadOnlySpan<String>)

將字串範圍結合成路徑。

Combine(String[])

將字串陣列結合成路徑。

Combine(String, String)

將兩個字串合併成路徑。

Combine(String, String, String)

將三個字串合併成路徑。

Combine(String, String, String, String)

將四個字串合併成路徑。

備註

這個方法的目的是將個別字串串連成代表檔案路徑的單一字串。 不過,如果第一個以外的自變數包含根路徑,則會忽略任何先前的路徑元件,而且傳回的字串會以該根路徑元件開頭。 作為 Combine 方法的替代方案,請考慮使用 JoinTryJoin 方法。

重要

此方法假設第一個自變數是絕對路徑,而下列自變數或自變數是相對路徑。 如果不是這種情況,特別是如果任何後續自變數是使用者輸入的字串,請改為呼叫 JoinTryJoin 方法。

Combine(ReadOnlySpan<String>)

將字串範圍結合成路徑。

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

參數

paths
ReadOnlySpan<String>

路徑的各個部分範圍。

傳回

合併的路徑。

適用於

Combine(String[])

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

將字串陣列結合成路徑。

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

參數

paths
String[]

路徑部分的陣列。

傳回

合併的路徑。

例外狀況

比 2.1 舊的 .NET Framework 和 .NET Core 版本:陣列中的其中一個字串包含 GetInvalidPathChars()中定義的一或多個無效字元。

陣列中的其中一個字串是 null

範例

下列範例會將字串數位結合成路徑。

string[] paths = {@"d:\archives", "2001", "media", "images"};
string fullPath = Path.Combine(paths);
Console.WriteLine(fullPath);
Dim paths As String() = {"d:\archives", "2001", "media", "images"}
Dim fullPath As String = Path.Combine(paths)
Console.WriteLine(fullPath)

備註

paths 應該是要合併之路徑部分的陣列。 如果其中一個後續路徑是絕對路徑,則合併作業會從該絕對路徑開始重設,捨棄所有先前合併的路徑。

如果 paths 中的任何專案,但最後一個專案不是磁碟驅動器,而且不會以 DirectorySeparatorCharAltDirectorySeparatorChar 字元結尾,則 Combine 方法會在該專案與下一個元素之間新增 DirectorySeparatorChar 字元。 請注意,如果專案以不適合目標平臺的路徑分隔符結尾,則 Combine 方法會保留原始路徑分隔符,並附加支援的分隔符。 當反斜杠作為路徑分隔符時,下列範例會比較 Windows 和 Unix 系統上的結果。

string[] paths = {@"d:\archives", "2001", "media", "images"};
string fullPath = Path.Combine(paths);
Console.WriteLine(fullPath);            

paths = new string[] {@"d:\archives\", @"2001\", "media", "images"};
fullPath = Path.Combine(paths);
Console.WriteLine(fullPath); 

paths = new string[] {"d:/archives/", "2001/", "media", "images"};
fullPath = Path.Combine(paths);
Console.WriteLine(fullPath); 
// The example displays the following output if run on a Windows system:
//    d:\archives\2001\media\images
//    d:\archives\2001\media\images
//    d:/archives/2001/media\images
//
// The example displays the following output if run on a Unix-based system:
//    d:\archives/2001/media/images
//    d:\archives\/2001\/media/images
//    d:/archives/2001/media/images
Dim paths As String() = { "d:\archives", "2001", "media", "images" }
Dim fullPath As String = Path.Combine(paths)
Console.WriteLine(fullPath)            

paths = { "d:\archives\", "2001\", "media", "images" }
fullPath = Path.Combine(paths)
Console.WriteLine(fullPath) 

paths = { "d:/archives/", "2001/", "media", "images" }
fullPath = Path.Combine(paths)
Console.WriteLine(fullPath) 
' The example displays the following output if run on a Windows system:
'    d:\archives\2001\media\images
'    d:\archives\2001\media\images
'    d:/archives/2001/media\images
'
' The example displays the following output if run on a Linux system:
'    d:\archives/2001/media/images
'    d:\archives\/2001\/media/images
'    d:/archives/2001/media/images

合併路徑中會省略長度為零的字串。

如果參數有空格符,則不會剖析這些參數。

比 2.1 舊的 .NET Framework 和 .NET Core 版本:並非所有無效的目錄和檔名字符都會被 Combine 方法解譯為無法接受,因為您可以使用這些字元來搜尋通配符。 例如,如果您從 Path.Combine("c:\\", "*.txt") 建立檔案,Path.Combine("c:\\", "*.txt") 可能是無效的,但它在搜尋字串中是有效的。 因此,Combine 方法已成功解譯。

另請參閱

適用於

Combine(String, String)

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

將兩個字串合併成路徑。

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

參數

path1
String

要結合的第一個路徑。

path2
String

要合併的第二個路徑。

傳回

合併的路徑。 如果其中一個指定的路徑是長度為零的字串,這個方法會傳回另一個路徑。 如果 path2 包含絕對路徑,這個方法會傳回 path2

例外狀況

2.1 之前的 .NET Framework 和 .NET Core 版本:path1path2 包含 GetInvalidPathChars()中定義的一或多個無效字元。

path1path2null

範例

下列範例示範如何在 Windows 上使用 Combine 方法。

using namespace System;
using namespace System::IO;
void CombinePaths( String^ p1, String^ p2 )
{
   try
   {
      String^ combination = Path::Combine( p1, p2 );
      Console::WriteLine( "When you combine '{0}' and '{1}', the result is: {2}'{3}'", p1, p2, Environment::NewLine, combination );
   }
   catch ( Exception^ e ) 
   {
      if (p1 == nullptr)
         p1 = "nullptr";
      if (p2 == nullptr)
         p2 = "nullptr";
      Console::WriteLine( "You cannot combine '{0}' and '{1}' because: {2}{3}", p1, p2, Environment::NewLine, e->Message );
   }

   Console::WriteLine();
}

int main()
{
   String^ path1 = "c:\\temp";
   String^ path2 = "subdir\\file.txt";
   String^ path3 = "c:\\temp.txt";
   String^ path4 = "c:^*&)(_=@#'\\^&#2.*(.txt";
   String^ path5 = "";
   String^ path6 = nullptr;
   CombinePaths( path1, path2 );
   CombinePaths( path1, path3 );
   CombinePaths( path3, path2 );
   CombinePaths( path4, path2 );
   CombinePaths( path5, path2 );
   CombinePaths( path6, path2 );
}
using System;
using System.IO;

public class ChangeExtensionTest
{
    public static void Main()
    {
        string path1 = "c:\\temp";
        string path2 = "subdir\\file.txt";
        string path3 = "c:\\temp.txt";
        string path4 = "c:^*&)(_=@#'\\^&#2.*(.txt";
        string path5 = "";

        CombinePaths(path1, path2);
        CombinePaths(path1, path3);
        CombinePaths(path3, path2);
        CombinePaths(path4, path2);
        CombinePaths(path5, path2);
    }

    private static void CombinePaths(string p1, string p2)
    {
        string combination = Path.Combine(p1, p2);

        Console.WriteLine("When you combine '{0}' and '{1}', the result is: {2}'{3}'",
                    p1, p2, Environment.NewLine, combination);

        Console.WriteLine();
    }
}
// This code produces output similar to the following:
//
// When you combine 'c:\temp' and 'subdir\file.txt', the result is:
// 'c:\temp\subdir\file.txt'
//
// When you combine 'c:\temp' and 'c:\temp.txt', the result is:
// 'c:\temp.txt'
//
// When you combine 'c:\temp.txt' and 'subdir\file.txt', the result is:
// 'c:\temp.txt\subdir\file.txt'
//
// When you combine 'c:^*&)(_=@#'\^&#2.*(.txt' and 'subdir\file.txt', the result is:
// 'c:^*&)(_=@#'\^&#2.*(.txt\subdir\file.txt'
//
// When you combine '' and 'subdir\file.txt', the result is:
// 'subdir\file.txt'
Imports System.IO

Public Class ChangeExtensionTest
    
    
    Public Shared Sub Main()
        Dim path1 As String = "c:\temp"
        Dim path2 As String = "subdir\file.txt"
        Dim path3 As String = "c:\temp.txt"
        Dim path4 As String = "c:^*&)(_=@#'\\^&#2.*(.txt"
        Dim path5 As String = ""
        Dim path6 As String = Nothing

        CombinePaths(path1, path2)
        CombinePaths(path1, path3)
        CombinePaths(path3, path2)
        CombinePaths(path4, path2)
        CombinePaths(path5, path2)
        CombinePaths(path6, path2)
    End Sub

    Private Shared Sub CombinePaths(p1 As String, p2 As String)
        
        Try
            Dim combination As String = Path.Combine(p1, p2)
            
            Console.WriteLine("When you combine '{0}' and '{1}', the result is: {2}'{3}'", p1, p2, Environment.NewLine, combination)
        Catch e As Exception
            If p1 = Nothing Then
                p1 = "Nothing"
            End If
            If p2 = Nothing Then
                p2 = "Nothing"
            End If
            Console.WriteLine("You cannot combine '{0}' and '{1}' because: {2}{3}", p1, p2, Environment.NewLine, e.Message)
        End Try
        
        Console.WriteLine()
    End Sub
End Class
' This code produces output similar to the following:
'
' When you combine 'c:\temp' and 'subdir\file.txt', the result is: 
' 'c:\temp\subdir\file.txt'
' 
' When you combine 'c:\temp' and 'c:\temp.txt', the result is: 
' 'c:\temp.txt'
' 
' When you combine 'c:\temp.txt' and 'subdir\file.txt', the result is: 
' 'c:\temp.txt\subdir\file.txt'
' 
' When you combine 'c:^*&)(_=@#'\^&#2.*(.txt' and 'subdir\file.txt', the result is: 
' 'c:^*&)(_=@#'\^&#2.*(.txt\subdir\file.txt'
' 
' When you combine '' and 'subdir\file.txt', the result is: 
' 'subdir\file.txt'
' 
' You cannot combine '' and 'subdir\file.txt' because: 
' Value cannot be null.
' Parameter name: path1

備註

如果 path1 不是磁碟驅動器參考(也就是 “C:” 或 “D:”),而且不會以 DirectorySeparatorCharAltDirectorySeparatorCharVolumeSeparatorChar中定義的有效分隔字元結尾,DirectorySeparatorChar 附加至串連之前 path1。 請注意,如果 path1 以不適合目標平臺的路徑分隔符結尾,Combine 方法會保留原始路徑分隔符,並附加支援的分隔符。 當反斜杠作為路徑分隔符時,下列範例會比較 Windows 和 Unix 系統上的結果。

var result = Path.Combine(@"C:\Pictures\", "Saved Pictures"); 
Console.WriteLine(result);
// The example displays the following output if run on a Windows system:
//    C:\Pictures\Saved Pictures
//
// The example displays the following output if run on a Unix-based system:
//    C:\Pictures\/Saved Pictures
Dim result = Path.Combine("C:\Pictures\", "Saved Pictures") 
Console.WriteLine(result)
' The example displays the following output if run on a Windows system:
'    C:\Pictures\Saved Pictures
'
' The example displays the following output if run on a Unix-based system:
'    C:\Pictures\/Saved Pictures

如果 path2 不包含根目錄(例如,如果 path2 不是以分隔符或磁碟驅動器規格開頭),則結果是兩個路徑的串連,具有交錯分隔符。 如果 path2 包含根目錄,則會傳回 path2

如果參數有空格符,則不會剖析這些參數。 因此,如果 path2 包含空格符(例如“\file.txt”),則 Combine 方法會將 path2 附加至 path1,而不是只傳回 path2

比 2.1 舊的 .NET Framework 和 .NET Core 版本:並非所有無效的目錄和檔名字符都會被 Combine 方法解譯為無法接受,因為您可以使用這些字元來搜尋通配符。 例如,如果您從 Path.Combine("c:\\", "*.txt") 建立檔案,Path.Combine("c:\\", "*.txt") 可能是無效的,但它在搜尋字串中是有效的。 因此,Combine 方法已成功解譯。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

Combine(String, String, String)

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

將三個字串合併成路徑。

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

參數

path1
String

要結合的第一個路徑。

path2
String

要合併的第二個路徑。

path3
String

要結合的第三個路徑。

傳回

合併的路徑。

例外狀況

2.1 之前的 .NET Framework 和 .NET Core 版本:path1path2path3 包含 GetInvalidPathChars()中定義的一或多個無效字元。

path1path2path3null

範例

下列範例結合三個路徑。

string p1 = @"d:\archives\";
string p2 = "media";
string p3 = "images";
string combined = Path.Combine(p1, p2, p3);
Console.WriteLine(combined);
Dim p1 As String = "d:\archives\"
Dim p2 As String = "media"
Dim p3 As String = "images"
Dim combined As String = Path.Combine(p1, p2, p3)
Console.WriteLine(combined)

備註

path1 應該是絕對路徑(例如 “d:\archives” 或 “\\archives\public” )。 如果 path2path3 也是絕對路徑,則合併作業會捨棄所有先前合併的路徑,並重設為該絕對路徑。

合併路徑中會省略長度為零的字串。

如果 path1path2 不是磁碟驅動器參考(也就是 “C:” 或 “D:”),而且不會以 DirectorySeparatorCharAltDirectorySeparatorCharVolumeSeparatorChar中定義的有效分隔字元結尾,DirectorySeparatorChar 附加至串連之前 path1path2。 請注意,如果 path1path2 結束於不適合目標平臺的路徑分隔符,Combine 方法會保留原始路徑分隔符,並附加支援的分隔符。 當反斜杠作為路徑分隔符時,下列範例會比較 Windows 和 Unix 系統上的結果。

var result = Path.Combine(@"C:\Pictures\", @"Saved Pictures\", "2019"); 
Console.WriteLine(result);
// The example displays the following output if run on a Windows system:
//    C:\Pictures\Saved Pictures\2019
//
// The example displays the following output if run on a Unix-based system:
//    C:\Pictures\/Saved Pictures\/2019
Dim result = Path.Combine("C:\Pictures\", "Saved Pictures\", "2019") 
Console.WriteLine(result)
' The example displays the following output if run on a Windows system:
'    C:\Pictures\Saved Pictures\2019
'
' The example displays the following output if run on a Unix-based system:
'    C:\Pictures\/Saved Pictures\/2019

如果 path2 不包含根目錄(例如,如果 path2 不是以分隔符或磁碟驅動器規格開頭),則結果是兩個路徑的串連,具有交錯分隔符。 如果 path2 包含根目錄,則會傳回 path2

如果參數有空格符,則不會剖析這些參數。 因此,如果 path2 包含空格符(例如“\file.txt”),則 Combine 方法會將 path2 附加至 path1

比 2.1 舊的 .NET Framework 和 .NET Core 版本:並非所有無效的目錄和檔名字符都會被 Combine 方法解譯為無法接受,因為您可以使用這些字元來搜尋通配符。 例如,如果您從 Path.Combine("c:\\", "*.txt") 建立檔案,Path.Combine("c:\\", "*.txt") 可能是無效的,但它在搜尋字串中是有效的。 因此,Combine 方法已成功解譯。

另請參閱

適用於

Combine(String, String, String, String)

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

將四個字串合併成路徑。

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

參數

path1
String

要結合的第一個路徑。

path2
String

要合併的第二個路徑。

path3
String

要結合的第三個路徑。

path4
String

要合併的第四個路徑。

傳回

合併的路徑。

例外狀況

2.1 之前的 .NET Framework 和 .NET Core 版本:path1path2path3path4 包含 GetInvalidPathChars()中定義的一或多個無效字元。

path1path2path3path4null

範例

下列範例會結合四個路徑。

string path1 = @"d:\archives\";
string path2 = "2001";
string path3 = "media";
string path4 = "images";
string combinedPath = Path.Combine(path1, path2, path3, path4);
Console.WriteLine(combinedPath);
Dim path1 As String = "d:\archives\"
Dim path2 As String = "2001"
Dim path3 As String = "media"
Dim path4 As String = "imaged"
Dim combinedPath As String = Path.Combine(path1, path2, path3, path4)
Console.WriteLine(combined)

備註

path1 應該是絕對路徑(例如 “d:\archives” 或 “\\archives\public” )。 如果其中一個後續路徑也是絕對路徑,則合併作業會捨棄所有先前合併的路徑,並重設為該絕對路徑。

合併路徑中會省略長度為零的字串。

如果 path1path2path3 不是磁碟驅動器參考(也就是 “C:” 或 “D:”),而且不會以 DirectorySeparatorCharAltDirectorySeparatorCharVolumeSeparatorChar中定義的有效分隔字元結尾,DirectorySeparatorChar 會在串連之前附加至它。 請注意,如果 path1path2path3 結尾為不適用於目標平台的路徑分隔符,Combine 方法會保留原始路徑分隔符,並附加支援的分隔符。 當反斜杠作為路徑分隔符時,下列範例會比較 Windows 和 Unix 系統上的結果。

var result = Path.Combine(@"C:\Pictures\", @"Saved Pictures\", @"2019\", @"Jan\"); 
Console.WriteLine(result);
// The example displays the following output if run on a Windows system:
//    C:\Pictures\Saved Pictures\2019\Jan\
//
// The example displays the following output if run on a Unix-based system:
//    C:\Pictures\Saved Pictures\2019\Jan\
Dim result = Path.Combine("C:\Pictures\", "Saved Pictures\", "2019\", "Jan\") 
Console.WriteLine(result)
' The example displays the following output if run on a Windows system:
'    C:\Pictures\Saved Pictures\2019\Jan\
'
' The example displays the following output if run on a Unix-based system:
'    C:\Pictures\Saved Pictures\2019\Jan\

如果 path2 不包含根目錄(例如,如果 path2 不是以分隔符或磁碟驅動器規格開頭),則結果是兩個路徑的串連,具有交錯分隔符。 如果 path2 包含根目錄,則會傳回 path2

如果參數有空格符,則不會剖析這些參數。 因此,如果 path2 包含空格符(例如“\file.txt”),則 Combine 方法會將 path2 附加至 path1

比 2.1 舊的 .NET Framework 和 .NET Core 版本:並非所有無效的目錄和檔名字符都會被 Combine 方法解譯為無法接受,因為您可以使用這些字元來搜尋通配符。 例如,如果您從 Path.Combine("c:\\", "*.txt") 建立檔案,Path.Combine("c:\\", "*.txt") 可能是無效的,但它在搜尋字串中是有效的。 因此,Combine 方法已成功解譯。

另請參閱

適用於