6

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.

A.R.
  • 15,405
  • 19
  • 77
  • 123
  • 1
    Run this code on Linux and see what you get! – oleksii Nov 08 '13 at 13:12
  • 1
    Also, please be aware that `Path.Combine` [treats starting slashes in its own way](http://stackoverflow.com/questions/5748032/why-does-path-combine-produce-this-result-with-a-relative-path). – Uwe Keim Nov 08 '13 at 13:15

4 Answers4

17

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.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 3
    I've lost count of the number of times I've seen paths with double backslashes in, because people didn't use `Path.Combine()`. It's a good job that the Windows API ignores extra backslashes in paths (other than at the start of a path)! – Matthew Watson Nov 08 '13 at 13:18
  • How does unix come into play? This probably sounds silly, but as a .NET developer, at what points are my applications running on unix? – A.R. Nov 08 '13 at 13:21
  • OK, That's what I thought. I guess I haven't gotten around to messing around with that yet. – A.R. Nov 08 '13 at 13:23
  • @DarrenHale Gotcha. While I have started using it, it is way less readable than concatenating slashes. – A.R. Nov 13 '13 at 17:06
  • @A.R.: You find `Path.Combine(root,sub1,p2,p3,p4,p5,fileName)` less readable than `root + "\\" + sub1 + "\\" + p2 + "\\" + p3 + "\\" + p4 + "\\" + p5 + "\\" fileName`? Not to mention that you need to maintain it or to find bugs if one of the variables contains incorrect slashes. – Tim Schmelter Nov 13 '13 at 17:14
  • @TimSchmelter Yes, I find it much less readable. Readability is a matter of opinion. The rest of the answer is objective, and I am now using Path.Combine (even though I have never had a problem doing it manually) – A.R. Nov 13 '13 at 17:51
  • @A.R.: "How does unix come into play? This probably sounds silly, but as a .NET developer, at what points are my applications running on unix" With .NET Core you can develop for multiple platforms – Tim Schmelter Feb 09 '23 at 15:49
9

When you don't know the first directory (e.g. it comes from user input), you may have C:\Directory or C:\Directory\ and Path.Combine will solve the trailing slash problem for you. It does have quirks with leading slashes for the next arguments though.

Second, while usually not a problem for most applications, with Path.Combine you aren't hard-coding the platform directory separator. For an application that can be deployed to other operating systems than windows this is convenient.

Anders Forsgren
  • 10,827
  • 4
  • 40
  • 77
4

Other platforms can use a different Separator, for example / instead of \ so the reason for not using \\ is to be S.O. independent

3

In this case, it does not really matter, but why don't you just write:

string myFilePath = "C:\\myDoc.txt";

The Path.Combine() method is useful if you are working with path variables and you don't want to check for backslashes (or whatever slashing required, depending on the platform):

string myFilePath = Path.Combine(path, filename);  
helb
  • 7,609
  • 8
  • 36
  • 58