Over the years, and again recently, I have heard discussion that everyone should use Path.Combine instead of joining strings together with "\\", for example:
string myFilePath = Path.Combine("c:", "myDoc.txt");
// vs.
string myFilePath = "C:" + "\\myDoc.txt";
I'm failing to see the benefit that the former version provides over the latter and I was hoping someone could explain.
Building a path with Path.Combine
is more readable and less error-prone. You don't need to think about directory separator chars(\\
or \
or /
on unix, ...) or if the first part of the path does or does not end in \
and whether the second part of the path does or does not start with \
.
You can concentrate on the important part, the directories and filenames. It's the same advantage that String.Format
has over string concatenation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With