LINQ Enumerable.Agregate with Path.Combine
Path.Combine accepts two parameters. Just two… So when we combine multiple path we have to deal with with nesting of Path.Combine:
string rootDir = "/root/app";
string folder1 = "users";
string folder2 = "mike/upload";
string filename = "text.xml";
var path = Path.Combine(rootDir, Path.Combine(folder1, Path.Combine(folder2, filename)));
There are 2 ways to handle this: 1. Use Enumerable.Agregate:
string rootDir = "/root/app";
string folder1 = "users";
string folder2 = "mike/upload";
string filename = "text.xml";
var path2 = new string[] { rootDir, folder1, folder2, filename }.Aggregate(Path.Combine);
- Or upgrade to .NET 4.0 where there is an overload with Path.Combine(params string[] paths) :) Have fun!