Oct 12 2007
System.IO.Path.Combine()
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().
I don’t know about wonderful. It really pales in comparison to what Java offers. For example, there are inadequate provisions for splitting or normalizing paths.
Ok, maybe “wonderful” is a bit OTT. But it’s a darn sight better than some of the code I’ve seen in the past.
Pet peeve: WHY doesn’t Path.Combine take a variable argument list? There’s no good reason. I have my own version in a util library that does, and it’s much cleaner.
(I could say the same thing about collection constructors as well… the literal initialization syntax leaves a lot to be desired.)
There are so many things like that throughout the .Net framework. I know there are many people out there that think that Java is much better, and maybe it is, but I personally prefer .Net.
[...] This is the cached version of http://www.gringod.com/2007/10/12/systemiopathcombine/#comment-27350 We are neither affiliated with the authors of this page nor responsible for its content. Comment on System.IO.Path.Combine() by Russell Mull [...]