Path.Combine 方法

定义

将多个字符串组合成一个路径。

重载

Combine(String[])

将字符串数组组合成一个路径。

Combine(String, String)

将两个字符串组合成一个路径。

Combine(String, String, String)

将三个字符串组合成一个路径。

Combine(String, String, String, String)

将四个字符串组合成一个路径。

注解

此方法旨在将单个字符串连接成表示文件路径的单个字符串。 但是,如果第一个参数以外的其他参数包含根路径,则忽略以前的任何路径组件,并且返回的字符串以该根路径组件开头。 作为 方法的 Combine 替代方法,请考虑使用 JoinTryJoin 方法。

重要

此方法假定第一个参数是绝对路径,以下参数是相对路径。 如果情况并非如此,特别是任何后续参数是用户输入的字符串,请改为调用 JoinTryJoin 方法。

Combine(String[])

将字符串数组组合成一个路径。

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 和 .NET Core 版本早于 2.1:数组中的一个字符串包含一个或多个在 中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)

将两个字符串组合成一个路径。

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 和 .NET Core 版本早于 2.1: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:”) 且不以 、 或 VolumeSeparatorCharDirectorySeparatorChar 中定义DirectorySeparatorCharAltDirectorySeparatorChar的有效分隔符结尾,在串联之前追加到 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 方法将追加 path2path1 而不是仅 path2返回 。

.NET Framework 和 2.1 之前的 .NET Core 版本:方法将目录和文件名的所有无效字符解释为不可Combine接受,因为可以将这些字符用于搜索通配符。 例如,如果要 Path.Combine("c:\\", "*.txt") 从中创建文件,则该文件可能无效,但它作为搜索字符串有效。 因此,它由 Combine 方法成功解释。

有关常见 I/O 任务的列表,请参阅 常见 I/O 任务

另请参阅

适用于

Combine(String, String, String)

将三个字符串组合成一个路径。

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 和 .NET Core 版本早于 2.1: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:”) 且不以 、 AltDirectorySeparatorCharDirectorySeparatorCharVolumeSeparatorCharDirectorySeparatorChar 中定义的有效分隔符结尾,则追加到 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

.NET Framework 和 2.1 之前的 .NET Core 版本:方法将目录和文件名的所有无效字符解释为不可Combine接受,因为可以将这些字符用于搜索通配符。 例如,如果要 Path.Combine("c:\\", "*.txt") 从中创建文件,则该文件可能无效,但它作为搜索字符串有效。 因此,它由 Combine 方法成功解释。

另请参阅

适用于

Combine(String, String, String, String)

将四个字符串组合成一个路径。

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 和 .NET Core 版本早于 2.1: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:”) 并且不以 、 或 VolumeSeparatorCharDirectorySeparatorChar 中定义的DirectorySeparatorCharAltDirectorySeparatorChar有效分隔符结尾,则连接之前会追加到它。 请注意,如果 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

.NET Framework 和 2.1 之前的 .NET Core 版本:方法将目录和文件名的所有无效字符解释为不可Combine接受,因为可以将这些字符用于搜索通配符。 例如,如果要 Path.Combine("c:\\", "*.txt") 从中创建文件,则该文件可能无效,但它作为搜索字符串有效。 因此,它由 Combine 方法成功解释。

另请参阅

适用于