Path.Combine 方法 (String, String)

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

命名空间:  System.IO
程序集:  mscorlib(在 mscorlib.dll 中)

 
public static string Combine(
	string path1,
	string path2
)

参数

path1
类型:System.String
要组合的第一个路径。
path2
类型:System.String
要组合的第二个路径。

返回值

类型:System.String
组合后的路径。 如果指定的路径之一是零长度字符串,则该方法返回另一个路径。 如果 path2 包含绝对路径,则该方法返回 path2 
异常条件
ArgumentException

path1 或 path2 包含 GetInvalidPathChars 中已定义的一个或多个无效字符。

ArgumentNullException

path1 或 path2 为 null

如果 path1 不是一个驱动器引用(即不是“C:”或“D:”)而且不是以 DirectorySeparatorCharAltDirectorySeparatorChar 或 VolumeSeparatorChar 中定义的有效分隔符结束,则在串联前将DirectorySeparatorChar 追加到 path1 中。

如果 path2 不包括根(例如,如果 path2 没有以分隔符或驱动器规格起始),则结果是两个路径的串联,具有介于其间的分隔符。 如果 path2 包括根,则返回 path2

如果参数有空格,则不会被分析。 因此,如果 path2 包括空白(例如“c:\”),则 Combine 方法会将 path2 追加到 path1 而不是仅返回 path2

不是目录和文件名的所有无效字符都被 Combine 方法解释为不可接受的,因为您可以将这些字符用于搜索通配符。 例如,尽管 Path.Combine("c:\", "*.txt") 可能是无效的(如果您要根据它创建一个文件),但它作为搜索字符串是有效的。 因此 Combine 方法成功解释它。

有关通用 I/O 任务的列表,请参见通用 I/O 任务

下面的代码示例演示如何在基于 Windows 的桌面平台上使用 Combine 方法。

 
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:	emp' and 'subdirfile.txt', the result is: 
// 'c:	empsubdirfile.txt'
// 
// When you combine 'c:	emp' and 'c:	emp.txt', the result is: 
// 'c:	emp.txt'
// 
// When you combine 'c:	emp.txt' and 'subdirfile.txt', the result is: 
// 'c:	emp.txtsubdirfile.txt'
// 
// When you combine 'c:^*&)(_=@#'^&#2.*(.txt' and 'subdirfile.txt', the result is: 
// 'c:^*&)(_=@#'^&#2.*(.txtsubdirfile.txt'
// 
// When you combine '' and 'subdirfile.txt', the result is: 
// 'subdirfile.txt'
// 
// You cannot combine '' and 'subdirfile.txt' because: 
// Value cannot be null.
// Parameter name: path1


.NET Framework

受以下版本支持:4.5、4、3.5、3.0、2.0、1.1、1.0

.NET Framework Client Profile

受以下版本支持:4、3.5 SP1

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008(不支持服务器核心角色), Windows Server 2008 R2(支持带 SP1 或更高版本的服务器核心角色;不支持 Itanium)

.NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求
原文地址:https://www.cnblogs.com/Italianetz/p/3392634.html