在C#中使用 路径拼接

C#将多个字符串组合成一个路径,可利用System.IO.Path类中的Combine(String,String)方法。

    命名空间为:using System.IO;

示例:

        string path1 = @"D:\temp";
        string path2 = "result.txt";
        string newPath = Path.Combine(path1, path2); 
        // newPath = "D:\temp\result.txt";
        //path.Combine方法拥有多个重载

如果需要返回地址的上一层目录
示例:

        string path="c:\admin\demo";
        Directory.SetCurrentDirectory(Directory.GetParent(path).FullName);
        path = Directory.GetCurrentDirectory(); //path="c:\admin";
        //返回几层则可以使用for循环

你可能感兴趣的:(C#,C#,路径拼接)