Oct 12 2007
Many, many times in my programming life I have seen people causing themselves all sorts of problems as the try and concatenate paths for file and directories. They’re always trying to work if the path already ends with a directory separator or not. And then there are the cross platform systems that try to workout what the directory separator should be.
With the release of the .Net platform, Microsoft gave developers that need to handle file system operations a wonderful, but underused, utility class: System.IO.Path. One of the methods on this class is Combine(). It take two arguments: path1 (string) and path2 (string), and intelligently combines them.
That’s it! No more messing around with string concatenation or endless if statements or figuring out if it should be a forward or backslash… just Path.Combine().