The header image for the 'Why I hate Path.Combine' post, sorry the platform doesn't allow alt text on headers

Why I hate Path.Combine

As most .NET developers will know there is a Path.Combine() method in System.IO which can be used to (you guessed it) combine two file paths. Unfortunately, it sucks; it sucks bad.

To demonstrate how badly Path.Combine() sucks here are some examples of its use.
some examples of Path.Combine use

As you can see it functions just as you would expect in the first three lines; but it sucks on the last three. Why would Microsoft not implement a path separator check; adding or removing the separator where applicable? A very good question in my opinion; so I have my own implementation.

using System;
using System.IO;
using System.Linq;

public static class Pathy {
	public static string Combine(string path1, params string[] paths) {
		return paths.Aggregate(path1, Combine);
	private static string Combine(string path, string path2) {
		char spliter = Path.DirectorySeparatorChar;
		if (path == null) {
			throw new ArgumentException("Base path can not be null", nameof(path));
		if (path2 == null) {
			throw new ArgumentException("Sub path can not be null", nameof(path2));
		path = path.Trim().TrimEnd(spliter);
		path += spliter;
		path += path2.Trim().TrimStart(spliter);
		return path;

Pathy.Combine() takes two or more paths in the same way that Path.Combine() does and correctly merges them based on the default Path.DirecotrySeperatorChar as used by the current environment.

Feel free to use and abuse this bit of code; it is provided with no warranty or guarantees. You can also find it on GitHub.

The header image used on this page was provided for free by Mike Enerio via thanks Mike!

About James Chorlton

I am a software developer from the South-West of England; I mostly work in .NET (c#) creating desktop, web, service, and backend software for the Legal and Health markets.