我如何从一个路径的相对路径另一个在C# [英] How do I get a relative path from one path to another in C#
本文介绍了我如何从一个路径的相对路径另一个在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望有一个内置的.NET方法要做到这一点,但我没有找到它。
I'm hoping there is a built in .NET method to do this, but I'm not finding it.
我有,我知道是同根驱动器上的两条路,我希望能够得到一个相对路径,从一边到另一边。
I have two paths that I know to be on the same root drive, I want to be able to get a relative path from one to the other.
string path1 = @"c:\dir1\dir2\";
string path2 = @"c:\dir1\dir3\file1.txt";
string relPath = MysteryFunctionThatShouldExist(path1, path2);
// relPath == "..\dir3\file1.txt"
这是否函数存在吗?如果不是这将是实现它的最好方法是什么?
Does this function exist? If not what would be the best way to implement it?
推荐答案
乌里
和
Uri path1 = new Uri(@"c:\dir1\dir2\");
Uri path2 = new Uri(@"c:\dir1\dir3\file1.txt");
Uri diff = path1.MakeRelativeUri(path2);
string relPath = diff.OriginalString;
这篇关于我如何从一个路径的相对路径另一个在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文