[英]Related paths in c#
I have this situation in c# application 我在C#应用程序中遇到这种情况
have 2 paths in my windows 我的窗户有2条路径
C:\\Projectos\\FrameworkCS\\CSoft.Core C:\\Projectos2\\CSoft.Core C:\\ Projectos \\ FrameworkCS \\ CSoft.Core C:\\ Projectos2 \\ CSoft.Core
and i need get related path of second related with first like this: 我需要获得与第二个相关的第二个相关路径,如下所示:
..\\..\\Projectos2\\CSoft.Core .. \\ .. \\ Projectos2 \\ CSoft.Core
Exists a way to do this in c# or some one have a function can help me 存在一种在C#中执行此操作的方法,或者有人有功能可以帮助我
Try MakeRelativeUri : 试试MakeRelativeUri :
Uri first = new Uri(@"C:\Projectos2\CSoft.Core");
Uri second = new Uri(@"C:\Projectos\FrameworkCS\CSoft.Core ");
string relativePath = second.MakeRelativeUri(first).ToString();
Result: 结果:
"../../Projectos2/CSoft.Core"
I would probably figure it out by splitting both using the '\\' as a separator. 我可能会通过使用'\\'作为分隔符来拆分来解决这个问题。 I would then count array items that were the same to get my common bond. 然后,我将数相同的数组项以获得共同的纽带。 Then I would build the destination path using the remaining items in the destination array and build the ..\\ string by counting the remaining items in the source. 然后,我将使用目标数组中的其余项构建目标路径,并通过计算源中的其余项来构建.. \\字符串。
Join the ..\\ with the CSoft.Core giving you ..\\CSoft.Core 通过CSoft.Core加入.. \\,为您提供.. \\ CSoft.Core
Addendum: I didn't realise you could use the URI.MakeRelativePath() method for this - don't bother reinventing the wheel if it's already been done elsewhere. 附录:我没有意识到您可以为此使用URI.MakeRelativePath()方法-如果已经在其他地方完成了此工作,则不要费心重新设计轮子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.