Path.Combine 方法 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将字符串合并到路径中。
重载
| Combine(ReadOnlySpan<String>) | 将字符串范围合并到路径中。 | 
| Combine(String[]) | 将字符串数组合并到路径中。 | 
| Combine(String, String) | 将两个字符串合并到一个路径中。 | 
| Combine(String, String, String) | 将三个字符串合并到一个路径中。 | 
| Combine(String, String, String, String) | 将四个字符串合并到一个路径中。 | 
注解
此方法旨在将单个字符串串联成表示文件路径的单个字符串。 但是,如果除第一个参数以外的参数包含根路径,则忽略任何以前的路径组件,并且返回的字符串以该根路径组件开头。 作为 Combine 方法的替代方法,请考虑使用 Join 或 TryJoin 方法(在 .NET Framework 中不可用)。
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> -> stringPublic Shared Function Combine (paths As ReadOnlySpan(Of String)) As String参数
- paths
- ReadOnlySpan<String>
路径的各个部分的跨度。
返回
组合路径。
适用于
Combine(String[])
- Source:
- Path.cs
- Source:
- Path.cs
- Source:
- Path.cs
将字符串数组合并到路径中。
public:
 static System::String ^ Combine(... cli::array <System::String ^> ^ paths);public static string Combine (params string[] paths);static member Combine : string[] -> stringPublic 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 中的任何元素,但最后一个元素不是驱动器,并且不以 DirectorySeparatorChar 或 AltDirectorySeparatorChar 字符结尾,则 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") 可能无效,但它作为搜索字符串有效。 因此,Combine 方法已成功解释它。
另请参阅
- 在 Windows 系统上 文件路径格式
适用于
Combine(String, String)
- Source:
- Path.cs
- Source:
- Path.cs
- Source:
- 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 -> stringPublic Shared Function Combine (path1 As String, path2 As String) As String参数
- path1
- String
要组合的第一个路径。
- path2
- String
要组合的第二个路径。
返回
组合路径。 如果其中一个指定路径是零长度字符串,则此方法返回另一个路径。 如果 path2 包含绝对路径,此方法将返回 path2。
例外
低于 2.1 的 .NET Framework 和 .NET Core 版本:path1 或 path2 包含 GetInvalidPathChars()中定义的一个或多个无效字符。
              path1 或 path2null。
示例
以下示例演示如何在 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:^*&)(_=@#'\\^.*(.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:^*&)(_=@#'\\^.*(.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:^*&)(_=@#'\^.*(.txt' and 'subdir\file.txt', the result is:
// 'c:^*&)(_=@#'\^.*(.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:^*&)(_=@#'\\^.*(.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:^*&)(_=@#'\^.*(.txt' and 'subdir\file.txt', the result is: 
' 'c:^*&)(_=@#'\^.*(.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:”),并且不以在连接之前 DirectorySeparatorChar、AltDirectorySeparatorChar或 VolumeSeparatorChar中定义的有效分隔符结尾,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") 可能无效,但它作为搜索字符串有效。 因此,Combine 方法已成功解释它。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
- 在 Windows 系统上 文件路径格式
- 文件和流 I/O
- 如何:从文件读取文本
- 如何:将文本写入文件
适用于
Combine(String, String, String)
- Source:
- Path.cs
- Source:
- Path.cs
- Source:
- 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 -> stringPublic 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 版本:path1、path2或 path3 包含一个或多个 GetInvalidPathChars()中定义的无效字符。
              path1、path2或 path3null。
示例
以下示例组合了三个路径。
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”)。 如果 path2 或 path3 也是绝对路径,则合并操作将放弃以前合并的所有路径并重置为该绝对路径。
从组合路径中省略零长度字符串。
如果 path1 或 path2 不是驱动器引用(即“C:”或“D:”),并且不会以在串联之前 DirectorySeparatorChar、AltDirectorySeparatorChar或 VolumeSeparatorChar中定义的有效分隔符结尾,DirectorySeparatorChar 将追加到 path1 或 path2。 请注意,如果 path1 或 path2 以不适合目标平台的路径分隔符结尾,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") 可能无效,但它作为搜索字符串有效。 因此,Combine 方法已成功解释它。
另请参阅
- 在 Windows 系统上 文件路径格式
适用于
Combine(String, String, String, String)
- Source:
- Path.cs
- Source:
- Path.cs
- Source:
- 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 -> stringPublic 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 版本:path1、path2、path3或 path4 包含一个或多个在 GetInvalidPathChars()中定义的无效字符。
              path1、path2、path3或 path4null。
示例
以下示例合并了四个路径。
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、path2或 path3 不是驱动器引用(即“C:”或“D:”),并且不会以 DirectorySeparatorChar、AltDirectorySeparatorChar或 VolumeSeparatorChar中定义的有效分隔符结尾,DirectorySeparatorChar 将追加到连接之前。 请注意,如果 path1、path2或 path3 以不适合目标平台的路径分隔符结尾,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") 可能无效,但它作为搜索字符串有效。 因此,Combine 方法已成功解释它。
另请参阅
- 在 Windows 系统上 文件路径格式