ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

PathCombine

Combines an IEnumerable<string> using Path.Combine(), which will use the separator character that is correct for the platform used. It is a shorter and more correct way to combine paths than just using + "\\" + . Requires "using System.IO;" at the top of your extension method class.

Source

public static string PathCombine(this IEnumerable<string> pathParts)
{
    string joinedPath = "";
    foreach(string pathPart in pathParts)
        joinedPath = Path.Combine(joinedPath,pathPart);

    return joinedPath;
}

Example

string path = new[] { s1, s2, s3 }.PathCombine();
string path = someIEnumerableString.PathCombine();

Author: Callum Rogers

Submitted on: 17 sep. 2009

Language: C#

Type: System.IEnumerable<string>

Views: 5572