我有两个文件路径,如下所示:
var path1 = "c:\dir\anotherdir";
var path2 = "c:\dir\anotherdir\yetanotherdir\dirception\file.zip";
var result = path2 - path1; //Wanted result: yetanotherdir\dirception\file.zip
我需要做的是从路径2中“删除”路径1。
最简单的解决方案是使用substr或其他方法,以“文本”方式从路径2中剪切出路径1。但我更愿意使用一些实际内置的C#函数来处理路径。
我尝试了这个:
var result = (new Uri(path1)).MakeRelativeUri(path2);
期望结果:yetanotherdir\dirception\file.zip
实际结果:anotherdir\yetanotherdir\dirception\file.zip
那么如何最好地达到我的目标呢?
注:该内容为关于IT技术的问题,涉及文件路径。