Reputation: 16864
I have this situation in c# application
have 2 paths in my windows
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
Exists a way to do this in c# or some one have a function can help me
Upvotes: 1
Views: 258
Reputation: 39836
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
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.
Upvotes: 0
Reputation: 838226
Try 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"
Upvotes: 5