35

I don't quite see the difference.

What could Path.Combine do better than perfectly working string concatenation?

I guess it's doing something very similar in the background.

Can anyone tell me why it is so often preferred?

user990423
  • 1,389
  • 2
  • 12
  • 31
Sossenbinder
  • 4,093
  • 3
  • 31
  • 66
  • 2
    First and biggest benefit of using Path.Combine is that, it will handle the slash issue on its own i.e. you need not to worry about adding '\' or '/' etc. About background process, read [here](https://startbigthinksmall.wordpress.com/2008/05/28/pathcombine-does-more-then-just-putting-a-between-to-strings-use-it/) – PM. Aug 18 '15 at 11:22
  • 3
    Code using `Path.Combine` would work unchanged on Linux & Mac when using mono. – Enigmativity Aug 18 '15 at 11:25

5 Answers5

39

Path.Combine uses the Path.PathSeparator and it checks whether the first path already has a separator at the end so it will not duplicate the separators. Additionally, it checks whether the path elements to combine have invalid chars.

niico
  • 9,173
  • 19
  • 72
  • 140
György Kőszeg
  • 15,070
  • 4
  • 31
  • 59
  • 1
    So in the end it's just an "upgraded" concatenation with extra checks? – Sossenbinder Aug 18 '15 at 11:27
  • 1
    I'm not sure what do you mean "upgraded", but you can simply check the sources here: http://referencesource.microsoft.com/#mscorlib/system/io/path.cs You should always use Combine instead of custom concatenation. – György Kőszeg Aug 18 '15 at 11:31
15

Path.Combine does more things than just a string concatenation. If you look at the source code;

  • Checks both paths has invalid character or not
  • Checks second parameter is root path or not
  • Checks last character of first path is director or alt directory or volume separator or not. If not, concatenate both string with directory separator between then
Soner Gönül
  • 94,086
  • 102
  • 195
  • 339
4

Here is the implementation

public static string Combine(string path1, string path2)
{
    if (path1 == null || path2 == null)
    {
        throw new ArgumentNullException((path1 == null) ? "path1" : "path2");
    }
    Path.CheckInvalidPathChars(path1, false);
    Path.CheckInvalidPathChars(path2, false);
    return Path.CombineNoChecks(path1, path2);
}

private static string CombineNoChecks(string path1, string path2)
{
    if (path2.Length == 0)
    {
        return path1;
    }
    if (path1.Length == 0)
    {
        return path2;
    }
    if (Path.IsPathRooted(path2))
    {
        return path2;
    }
    char c = path1[path1.Length - 1];
    if (c != Path.DirectorySeparatorChar && c != Path.AltDirectorySeparatorChar && c != Path.VolumeSeparatorChar)
    {
        return path1 + Path.DirectorySeparatorChar + path2;
    }
    return path1 + path2;
}
Chamika Sandamal
  • 22,932
  • 5
  • 63
  • 83
3

According to this documentation Path.Combine internally performs a string concatenation using +-Operator.

 private static String CombineNoChecks(String path1, String path2) {
        if (path2.Length == 0)
            return path1;

        if (path1.Length == 0)
            return path2;

        if (IsPathRooted(path2))
            return path2;

        char ch = path1[path1.Length - 1];
        if (ch != DirectorySeparatorChar && ch != AltDirectorySeparatorChar && ch != VolumeSeparatorChar) 
            return path1 + DirectorySeparatorCharAsString + path2;
        return path1 + path2;
    }
bwegs
  • 3,731
  • 2
  • 29
  • 32
ckruczek
  • 2,228
  • 2
  • 21
  • 22
2

You avoid double path separators. If one path element already has a leading backslash. Path.Combine checks for that and ensures that only one backslash is present.

MBulli
  • 1,599
  • 1
  • 22
  • 35