Does there exist a method in C# to get the relative path given two absolute path inputs?
That is I would have two inputs (with the first folder as the base) such as
c:\temp1\adam\
and
c:\temp1\jamie\
Then the output would be
..\jamie\
Methods are also known as the functions of a class. They are primarily used to provide code reusability, which saves computational time and makes the code well structured and clean. Methods also increase code readability since the same set of operations do not need to be written again and again.
Classes and their member functions (or methods) are integral features of the object-oriented C++ programming language. By tying these functions to an object's namespace, class methods make your C++ code modular and reusable.
Not sure if there is a better way, but this will work:
var file1 = @"c:\temp1\adam\";
var file2 = @"c:\temp1\jamie\";
var result = new Uri(file1)
.MakeRelativeUri(new Uri(file2))
.ToString()
.Replace("/", "\\");
this is simple. Steps:
c:\temp1\
)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With