views:

171

answers:

4

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

+4  A: 

I think this requires a-priori knowledge that certain folders are the same, something you cannot safely infer from just the path (given that it's not absolute).

You'd have to write some custom logic yourself to do the combining of these paths.

John Weldon
+1: without a known anchor point, the same name doesn't necessarily mean the same folder in the hierarchy.
Ben M
Assuming that part of the value of path1 is always know (e.g. "/System/Configuration/....", it is possible.
Ioannis
@loannis, but that would require that you *know* that there is a certain amount of overlap and that your folder structure doesn't include other paths that have similar names.
John Weldon
Yes that is true but I have this information in my case.
Ioannis
+3  A: 

Provided that the two strings are always in the same format as in your example, this should work:

string path1 = "/System/Configuration/Panels/Alpha";
string path2 = "Panels/Alpha/Data";

var x = path1.Split('/');
var y = path2.Split('/');

string result = Enumerable.Range(0, x.Count())

                          .Where(i => x.Skip(i)
                                       .SequenceEqual(y.Take(x.Skip(i)
                                                              .Count())))

                          .Select(i => string.Join("/", x.Take(i)
                                                         .Concat(y)))

                          .LastOrDefault();

// result == "/System/Configuration/Panels/Alpha/Data"

For path1 = "/System/a/b/a/b" and path2 = "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.

dtb
+1... Well done!
p.campbell
Thank you that is very elegant code. I didn't want to use for. :)
Ioannis
The caveat being that you have to *assume* that "Panels/Alpha" are the same folders as "/System/Configuration/*Panels/Alpha*"
John Weldon
I wouldn't call this elegant, in fact I think it's rather ugly. No offence to the author is intended, but if I were assigned to maintain this particular application, I would prefer that the author of written it in standard, imperative, code.It's cool code in an academic sense, however.
nlaq
Well looks like F# :)
Ioannis
I *would* call this elegant, but I think imperative-only folks would have to learn and wrap their brain around this before being able to use it.
John Weldon
+1  A: 

Try this...

var p1 = path1.Split('/');
var p2 = path2.Split('/');

result = p1.Union(p1);

It uses System.Linq, and can easily be packaged into an extension method.

Of course, it assumes something about the values of path1 and path2.

pblasucci
While my solution probably isn't perfect as well, this fails for a lot more cases. For example, if `path1 = "/System/Panels/Configuration/Panels/Alpha"` and `path2 = "Panels/Alpha/Data"`, the result is `"/System/Panels/Configuration/Alpha/Data"`.
dtb
Agreed... though really, its impossible to provide a "best-fit" solution without having some additional context.
pblasucci
A: 
Steve Dennis