Path.Combine Path.Combine Path.Combine Path.Combine Method

定義

將多個字串合併為一個路徑。Combines strings into a path.

多載

Combine(String[]) Combine(String[]) Combine(String[]) Combine(String[])

將一個字串陣列合併為單一路徑。Combines an array of strings into a path.

Combine(String, String) Combine(String, String) Combine(String, String) Combine(String, String)

將兩個字串合併為一個路徑。Combines two strings into a path.

Combine(String, String, String) Combine(String, String, String) Combine(String, String, String) Combine(String, String, String)

將三個字串合併為一個路徑。Combines three strings into a path.

Combine(String, String, String, String) Combine(String, String, String, String) Combine(String, String, String, String) Combine(String, String, String, String)

將四個字串合併為一個路徑。Combines four strings into a path.

備註

這個方法的目的是要將個別字串串連成代表檔案路徑的單一字串。This method is intended to concatenate individual strings into a single string that represents a file path. 不過, 如果第一個引數包含根路徑, 則會忽略任何先前的路徑元件, 而傳回的字串會以該根路徑元件作為開頭。However, if an argument other than the first contains a rooted path, any previous path components are ignored, and the returned string begins with that rooted path component. 做為Combine方法的替代方式, 請考慮Join使用或TryJoin方法。As an alternative to the Combine method, consider using the Join or TryJoin methods.

重要

這個方法假設第一個引數是絕對路徑, 而下列引數或引數是相對路徑。This method assumes that the first argument is an absolute path and that the following argument or arguments are relative paths. 如果不是這種情況, 特別是當任何後續引數是使用者輸入的字串時, 請JoinTryJoin為呼叫或方法。If this is not the case, and particularly if any subsequent arguments are strings input by the user, call the Join or TryJoin method instead.

Combine(String[]) Combine(String[]) Combine(String[]) Combine(String[])

將一個字串陣列合併為單一路徑。Combines an array of strings into a path.

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

路徑中各部分的陣列。An array of parts of the path.

傳回

合併的路徑。The combined paths.

例外狀況

陣列中的一個字串包含在 GetInvalidPathChars() 中定義的一或多個無效字元。One of the strings in the array contains one or more of the invalid characters defined in GetInvalidPathChars().

陣列中的一個字串為 nullOne of the strings in the array is null.

範例

下列範例會將字串陣列結合成一個路徑。The following example combines an array of strings into a path.

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 should be an array of the parts of the path to combine. 如果後續的其中一個路徑是絕對路徑, 則合併作業會從該絕對路徑開始重設, 並捨棄所有先前的合併路徑。If the one of the subsequent paths is an absolute path, then the combine operation resets starting with that absolute path, discarding all previous combined paths.

如果 paths 中除了最後一個元素以外的任何元素不是磁碟機且結尾不是 DirectorySeparatorCharAltDirectorySeparatorChar 字元,Combine 方法會將 DirectorySeparatorChar 字元加在該元素與下一個字元之間。If any element in paths but the last one is not a drive and does not end with either the DirectorySeparatorChar or the AltDirectorySeparatorChar character, the Combine method adds a DirectorySeparatorChar character between that element and the next one. 請注意,如果元素結尾不是適用於目標平台路徑分隔符號字元,Combine 方法會保留原始的路徑分隔符號字元,並將附加支援的字元。Note that, if the element ends in a path separator character that is not appropriate for the target platform, the Combine method preserves the original path separator character and appends a supported one. 下列範例會比較使用反斜線當做路徑分隔符號字元時 Windows 和 unix 系統上的結果。The following example compares the result on Windows and Unix-based systems when the backslash is used as a path separator character.

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    

結合的路徑中會省略長度為零的字串。Zero-length strings are omitted from the combined path.

如果參數有空白字元, 則不會剖析它們。The parameters are not parsed if they have white space.

並非所有不正確目錄和檔案名的字元都會被視為無法接受的Combine方法, 因為您可以使用這些字元來搜尋萬用字元。Not all invalid characters for directory and file names are interpreted as unacceptable by the Combine method, because you can use these characters for search wildcard characters. 例如, 如果您Path.Combine("c:\\", "*.txt")要從檔案建立檔案, 雖然可能無效, 但它是有效的搜尋字串。For example, while Path.Combine("c:\\", "*.txt") might be invalid if you were to create a file from it, it is valid as a search string. 因此, Combine方法會成功地將它解讀。It is therefore successfully interpreted by the Combine method.

另請參閱

Combine(String, String) Combine(String, String) Combine(String, String) Combine(String, String)

將兩個字串合併為一個路徑。Combines two strings into a path.

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 String String String

要合併的第一個路徑。The first path to combine.

path2
String String String String

要合併的第二個路徑。The second path to combine.

傳回

合併的路徑。The combined paths. 如果指定的其中一個路徑是長度為零的字串,這個方法會傳回其他路徑。If one of the specified paths is a zero-length string, this method returns the other path. 如果 path2 包含絕對路徑,這個方法會傳回 path2If path2 contains an absolute path, this method returns path2.

例外狀況

path1path2 含有在 GetInvalidPathChars() 中定義的一或多個不正確字元。path1 or path2 contains one or more of the invalid characters defined in GetInvalidPathChars().

path1path2nullpath1 or path2 is null.

範例

下列範例示範如何在以Combine Windows 為基礎的桌面平臺上使用方法。The following example demonstrates using the Combine method on a Windows-based desktop platform.

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 = "";
        string path6 = null;

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

    private static 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 == null)
                p1 = "null";
            if (p2 == null)
                p2 = "null";
            Console.WriteLine("You cannot combine '{0}' and '{1}' because: {2}{3}",
                        p1, p2, Environment.NewLine, e.Message);
        }

        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'
// 
// You cannot combine '' and 'subdir\file.txt' because: 
// Value cannot be null.
// Parameter name: path1
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 'Main

    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 'CombinePaths
End Class 'ChangeExtensionTest
' 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:"), 而且不是以、 VolumeSeparatorChar AltDirectorySeparatorChar或中DirectorySeparatorChar定義的有效分隔符號做為結尾, DirectorySeparatorChar則會在path1串連之前附加至。If path1 is not a drive reference (that is, "C:" or "D:") and does not end with a valid separator character as defined in DirectorySeparatorChar, AltDirectorySeparatorChar, or VolumeSeparatorChar, DirectorySeparatorChar is appended to path1 before concatenation. 請注意, path1如果在不適合目標平臺的路徑分隔符號中結束Combine , 方法會保留原始路徑分隔符號, 並附加支援的符號。Note that if path1 ends in a path separator character that is not appropriate for the target platform, the Combine method preserves the original path separator character and appends a supported one. 下列範例會比較使用反斜線當做路徑分隔符號字元時 Windows 和 unix 系統上的結果。The following example compares the result on Windows and Unix-based systems when the backslash is used as a path separator character.

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開頭不是分隔符號或磁片磁碟機規格), 則結果會是兩個路徑的串連, 並包含中間的分隔符號。If path2 does not include a root (for example, if path2 does not start with a separator character or a drive specification), the result is a concatenation of the two paths, with an intervening separator character. 如果path2包含根, path2則會傳回。If path2 includes a root, path2 is returned.

如果參數有空白字元, 則不會剖析它們。The parameters are not parsed if they have white space. 因此, 如果path2包含空白字元 (例如 "\file.txt") Combine , 方法會將附加path2path1 , 而不是只path2傳回。Therefore, if path2 includes white space (for example, " \file.txt "), the Combine method appends path2 to path1 instead of returning only path2.

並非所有不正確目錄和檔案名的字元都會被視為無法接受的Combine方法, 因為您可以使用這些字元來搜尋萬用字元。Not all invalid characters for directory and file names are interpreted as unacceptable by the Combine method, because you can use these characters for search wildcard characters. 例如, 如果您Path.Combine("c:\\", "*.txt")要從檔案建立檔案, 雖然可能無效, 但它是有效的搜尋字串。For example, while Path.Combine("c:\\", "*.txt") might be invalid if you were to create a file from it, it is valid as a search string. 因此, Combine方法會成功地將它解讀。It is therefore successfully interpreted by the Combine method.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

另請參閱

Combine(String, String, String) Combine(String, String, String) Combine(String, String, String) Combine(String, String, String)

將三個字串合併為一個路徑。Combines three strings into a path.

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 String String String

要合併的第一個路徑。The first path to combine.

path2
String String String String

要合併的第二個路徑。The second path to combine.

path3
String String String String

要合併的第三個路徑。The third path to combine.

傳回

合併的路徑。The combined paths.

例外狀況

path1path2path3 包含一或多個在 GetInvalidPathChars() 中定義的無效字元。path1, path2, or path3 contains one or more of the invalid characters defined in GetInvalidPathChars().

path1path2path3nullpath1, path2, or path3 is null.

範例

下列範例結合了三個路徑。The following example combines three paths.

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")。path1 should be an absolute path (for example, "d:\archives" or "\\archives\public"). 如果path2path3也是絕對路徑, 則合併作業會捨棄所有先前合併的路徑, 並重設為該絕對路徑。If path2 or path3 is also an absolute path, the combine operation discards all previously combined paths and resets to that absolute path.

結合的路徑中會省略長度為零的字串。Zero-length strings are omitted from the combined path.

如果path1AltDirectorySeparatorChar DirectorySeparatorChar DirectorySeparatorChar path1 VolumeSeparatorChar不是磁片磁碟機參考 (也就是 "C:" 或 "D:"), 而且不是以、或中定義的有效分隔符號做為結尾, 則會將附加至或path2 path2串連之前。If path1 or path2 is not a drive reference (that is, "C:" or "D:") and does not end with a valid separator character as defined in DirectorySeparatorChar, AltDirectorySeparatorChar, or VolumeSeparatorChar, DirectorySeparatorChar is appended to path1 or path2 before concatenation. 請注意, path1如果path2或結束的路徑分隔符號不適合目標平臺, Combine方法會保留原始的路徑分隔符號, 並附加支援的符號。Note that if path1 or path2 ends in a path separator character that is not appropriate for the target platform, the Combine method preserves the original path separator character and appends a supported one. 下列範例會比較使用反斜線當做路徑分隔符號字元時 Windows 和 unix 系統上的結果。The following example compares the result on Windows and Unix-based systems when the backslash is used as a path separator character.

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開頭不是分隔符號或磁片磁碟機規格), 則結果會是兩個路徑的串連, 並包含中間的分隔符號。If path2 does not include a root (for example, if path2 does not start with a separator character or a drive specification), the result is a concatenation of the two paths, with an intervening separator character. 如果path2包含根, path2則會傳回。If path2 includes a root, path2 is returned.

如果參數有空白字元, 則不會剖析它們。The parameters are not parsed if they have white space. 因此, 如果path2包含空白字元 (例如 "\file.txt"), 方法會將Combine附加path2path1Therefore, if path2 includes white space (for example, " \file.txt "), the Combine method appends path2 to path1.

並非所有不正確目錄和檔案名的字元都會被視為無法接受的Combine方法, 因為您可以使用這些字元來搜尋萬用字元。Not all invalid characters for directory and file names are interpreted as unacceptable by the Combine method, because you can use these characters for search wildcard characters. 例如, 如果您Path.Combine("c:\\", "*.txt")要從檔案建立檔案, 雖然可能無效, 但它是有效的搜尋字串。For example, while Path.Combine("c:\\", "*.txt") might be invalid if you were to create a file from it, it is valid as a search string. 因此, Combine方法會成功地將它解讀。It is therefore successfully interpreted by the Combine method.

另請參閱

Combine(String, String, String, String) Combine(String, String, String, String) Combine(String, String, String, String) Combine(String, String, String, String)

將四個字串合併為一個路徑。Combines four strings into a path.

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 String String String

要合併的第一個路徑。The first path to combine.

path2
String String String String

要合併的第二個路徑。The second path to combine.

path3
String String String String

要合併的第三個路徑。The third path to combine.

path4
String String String String

要合併的第四個路徑。The fourth path to combine.

傳回

合併的路徑。The combined paths.

例外狀況

path1path2path3path4 包含一或多個在 GetInvalidPathChars() 中定義的無效字元。path1, path2, path3, or path4 contains one or more of the invalid characters defined in GetInvalidPathChars().

path1path2path3path4nullpath1, path2, path3, or path4 is null.

範例

下列範例結合了四個路徑。The following example combines four paths.

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")。如果其中一個後續路徑也是絕對路徑, 則合併作業會捨棄所有先前合併的路徑, 並重設為該絕對路徑。path1 should be an absolute path (for example, "d:\archives" or "\\archives\public").If one of the subsequent paths is also an absolute path, the combine operation discards all previously combined paths and resets to that absolute path.

結合的路徑中會省略長度為零的字串。Zero-length strings are omitted from the combined path.

如果path1AltDirectorySeparatorChar DirectorySeparatorChar VolumeSeparatorChar DirectorySeparatorChar或不是磁片磁碟機參考 (也就是 "C:" 或 "D:"), 而且不是以、或中定義的有效分隔符號做為結尾, 則會將它附加至該字元path3 path2串連之前。If path1, path2, or path3 is not a drive reference (that is, "C:" or "D:") and does not end with a valid separator character as defined in DirectorySeparatorChar, AltDirectorySeparatorChar, or VolumeSeparatorChar, DirectorySeparatorChar is appended to it before concatenation. 請注意, path1如果path2、或path3Combine不適合目標平臺的路徑分隔符號中結束, 方法會保留原始路徑分隔符號, 並附加支援的符號。Note that if path1, path2, or path3 ends in a path separator character that is not appropriate for the target platform, the Combine method preserves the original path separator character and appends a supported one. 下列範例會比較使用反斜線當做路徑分隔符號字元時 Windows 和 unix 系統上的結果。The following example compares the result on Windows and Unix-based systems when the backslash is used as a path separator character.

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開頭不是分隔符號或磁片磁碟機規格), 則結果會是兩個路徑的串連, 並包含中間的分隔符號。If path2 does not include a root (for example, if path2 does not start with a separator character or a drive specification), the result is a concatenation of the two paths, with an intervening separator character. 如果path2包含根, path2則會傳回。If path2 includes a root, path2 is returned.

如果參數有空白字元, 則不會剖析它們。The parameters are not parsed if they have white space. 因此, 如果path2包含空白字元 (例如 "\file.txt"), 方法會將Combine附加path2path1Therefore, if path2 includes white space (for example, " \file.txt "), the Combine method appends path2 to path1.

並非所有不正確目錄和檔案名的字元都會被視為無法接受的Combine方法, 因為您可以使用這些字元來搜尋萬用字元。Not all invalid characters for directory and file names are interpreted as unacceptable by the Combine method, because you can use these characters for search wildcard characters. 例如, 如果您Path.Combine("c:\\", "*.txt")要從檔案建立檔案, 雖然可能無效, 但它是有效的搜尋字串。For example, while Path.Combine("c:\\", "*.txt") might be invalid if you were to create a file from it, it is valid as a search string. 因此, Combine方法會成功地將它解讀。It is therefore successfully interpreted by the Combine method.

另請參閱

適用於