I want to combine two relative paths in C#.
For example:
string path1 = "/System/Configuration/Panels/Alpha";
string path2 = "Panels/Alpha/Data";
I want to return
string result = "/System/Configuration/Panels/Alpha/Data";
I can implement this by splitting the second array and compare it in a for loop but I was wondering if there is something similar to Path.Combine available or if this can be accomplished with regular expressions or Linq?
Thanks
Provided that the two strings are always in the same format as in your example, this should work:
For
path1 = "/System/a/b/a/b"andpath2 = "a/b/a/b/c"the result is"/System/a/b/a/b/a/b/c". You can change LastOrDefault to FirstOrDefault to get"/System/a/b/a/b/c"instead.Note that this algorithm essentially creates all possible combinations of the two paths and isn’t particularly efficient.