【问题标题】:Path.Combine two relative path strings to create new relative pathPath. 组合两个相对路径字符串以创建新的相对路径
【发布时间】:2019-03-12 10:31:58
【问题描述】:

我正在尝试使用 Path.Combine 连接两个相对路径以创建另一个 相对(非绝对)路径。

string path1=@"rootDir\DirA\DirAA";
string path2=@"..\..\DirB";
Console.WriteLine(Path.Combine(path1, path2));
//I get: rootDir\DirA\DirAA\..\..\DirB
Console.WriteLine(Path.GetFullPath(Path.Combine(path1, path2)));
//I get: C:User\rootDir\DirB

我真正想要的是

//rootDir\DirB

有什么方法可以使用 Path 来实现吗?

【问题讨论】:

  • 你能把路径字符串在“\”处拆分,然后组合你想要的部分吗?
  • @blueprogrammer 不,我正在从两个相对路径的组合中寻找相对路径,而不是绝对路径。

标签: c# .net path filesystems


【解决方案1】:

试试这个:

Path.GetFullPath(Path.Combine(path1, path2))
    .Substring(Directory.GetCurrentDirectory().Length + 1);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 2014-11-24
    • 2012-10-16
    • 2011-08-11
    • 2020-05-26
    • 2011-08-08
    相关资源
    最近更新 更多