获得2条路径的差异

时间:2014-10-19 17:46:26

标签: c# winforms

说我有

string path1 = "C:\\apples";
string path2 = "C:\apples\blah\meh.jpg";

我怎么能得到:

string diff = "\blah\meh.jpg";

由于

1 个答案:

答案 0 :(得分:0)

如果Path实际上包含双斜杠(通常不会发生):

  1. \\替换为\

  2. 中的path1
  3. path1替换为Empty String

    中的path2
    string diff = path2.Replace(path1.Replace(@"\\", @"\"), "");
    
  4. 否则:

    string diff = path2.Replace(path1, "");