Oct 12 2007

System.IO.Path.Combine()

Published by gringod at 1:59 pm under .NET, Programming

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().

kick it on DotNetKicks.com

5 responses so far

5 Responses to “System.IO.Path.Combine()”

  1. Jeff Brownon 16 Oct 2007 at 11:21 pm

    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.

  2. gringodon 17 Oct 2007 at 9:06 am

    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.

  3. Russell Mullon 18 Oct 2007 at 4:41 pm

    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.)

  4. gringodon 18 Oct 2007 at 10:50 pm

    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.

  5. [...] 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 [...]

Trackback URI | Comments RSS |

Leave a Reply