Path.Combine 方法

定義

將多個字串合併為一個路徑。

多載

Combine(String[])

將一個字串陣列合併為單一路徑。

Combine(String, String)

將兩個字串合併為一個路徑。

Combine(String, String, String)

將三個字串合併為一個路徑。

Combine(String, String, String, String)

將四個字串合併為一個路徑。

備註

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

重要

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

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[]

路徑中各部分的陣列。

傳回

合併的路徑。

例外狀況

.NET Framework 和 2.1 之前的 .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

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

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

.NET Framework 和 2.1 之前的 .NET Core 版本:不是方法無法接受Combine目錄和檔名的所有無效字元,因為您可以使用這些字元來搜尋通配符。 例如, 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

例外狀況

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

path1path2null

範例

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

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:”) ,且結尾不會以 、 AltDirectorySeparatorCharVolumeSeparatorChar中所DirectorySeparatorChar定義的有效分隔符結尾,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傳回 。

.NET Framework 和 2.1 之前的 .NET Core 版本:不是方法無法接受Combine目錄和檔名的所有無效字元,因為您可以使用這些字元來搜尋通配符。 例如, 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

要合併的第三個路徑。

傳回

合併的路徑。

例外狀況

.NET Framework 和 2.1 之前的 .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:”) ,且結尾不是以 、 AltDirectorySeparatorCharDirectorySeparatorCharVolumeSeparatorChar中所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 」) ,方法會Combinepath2附加至 path1

.NET Framework 和 2.1 之前的 .NET Core 版本:不是方法無法接受Combine目錄和檔名的所有無效字元,因為您可以使用這些字元來搜尋通配符。 例如, 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

要合併的第四個路徑。

傳回

合併的路徑。

例外狀況

.NET Framework 和 2.1 之前的 .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:”) ,而且結尾不是在 、 AltDirectorySeparatorCharVolumeSeparatorCharDirectorySeparatorCharDirectorySeparatorChar定義的有效分隔符字元,則會在串連之前附加至它。 請注意,如果 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 」) ,方法會Combinepath2附加至 path1

.NET Framework 和 2.1 之前的 .NET Core 版本:不是方法無法接受Combine目錄和檔名的所有無效字元,因為您可以使用這些字元來搜尋通配符。 例如, Path.Combine("c:\\", "*.txt") 如果您從檔案建立檔案,則雖然 可能無效,但其有效為搜尋字串。 因此,此方法已成功解譯 Combine

另請參閱

適用於