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?
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?
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.
Path.Combine
does more things than just a string concatenation. If you look at the source code;
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;
}
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;
}
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.
System.IO.Path.Combine() Automatically combines multiple paths to a single string by using the correct path separator i.e. forward slash '/' or backward slash '' compatible to the operating systems OS. On the other hand, using string concatenation will do the same but we will need to explicitly (manually) add correct path separator after or before the paths respectively to the paths. forexample:
string virtualPath = "/data/directory" +"/" + "video.mp4";
while using Path.Combine(),
string virtualPath = Path.Combine("/data/directory", "video.mp4");
Both the Path.Combine() and string concatenation method produce the same result but Path.Combine() method offers a more elegant method to combine paths.
© 2022 - 2024 — McMap. All rights reserved.
Path.Combine
would work unchanged on Linux & Mac when using mono. – Wino