Path.Combine 方法 (String, String)

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

 

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

C#
C++
F#
VB
 
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 方法。

C#
C++
VB
 
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


.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/Sunnyj/p/3392634.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值