This is an interesting question:
You could, of course, write something like:
string finalPath = String.Format("{0}\\file.txt", folder);
To achieve the desired result.
Using ILSpy , itβs better to understand why Path.Combine .
The overload you are calling:
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); }
The benefits are obvious; firstly, the function checks for null values ββand throws an appropriate exception. Then it checks for invalid characters in any of the arguments and throws an appropriate exception. Once it is satisfied, it calls Path.CombineNoChecks:
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; }
The most interesting here are the characters that it supports;
Path.DirectorySeparatorChar = "\\" Path.AltDirectorySeparatorChar = "/" Path.VolumeSeparatorChar = ":"
Thus, it will also support paths in which the separator does not fit (for example, from the file://C:/blah urn)
In short, this is better because it gives you validation, portability (the 3 constants shown above can be defined for each database base) and supports more than one type of path that you usually encounter.