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

 

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

 
public static string Combine(
	string path1,
	string path2
)

参数

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

返回值

类型:System.String
path2。 
异常 条件
ArgumentException

GetInvalidPathChars 中已定义的一个或多个无效字符。

ArgumentNullException

null

path1 中。

path2

path2

Combine 方法成功解释它。

通用 I/O 任务

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:\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 系统要求

相关文章:

  • 2021-11-15
  • 2022-12-23
  • 2021-06-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-26
  • 2021-07-03
  • 2021-07-22
  • 2022-12-23
  • 2022-12-23
  • 2021-07-14
  • 2022-02-11
相关资源
相似解决方案