If you already have an array or IEnumerable, you can do it on one line ...
// I'm assuming that you've got an array or IEnumerable<T> from somewhere var paths = new string[] { path1, path2, path3, path4, path5, path6 }; string result = paths.Aggregate(Path.Combine);
If not, what about writing your own extension method for the string ...
public static class PathExtension { public static string CombinePathWith(this string path1, string path2) { return Path.Combine(path1, path2); } }
... which allows you to link them like this:
string result = path1.CombinePathWith(path2) .CombinePathWith(path3) .CombinePathWith(path4) .CombinePathWith(path5) .CombinePathWith(path6);
Martin Peck Apr 17 '09 at 14:51 2009-04-17 14:51
source share