Hidden .NET Gems – Path.Combine


I’ve learned about the magic of the method Combine from Path a few months ago. Usually, even in production code, we tend to use string concatenation through either directly using ‘+’ or StringBuilder. Thing is, you do not want to directly use string concatenation to build your path. Try to think about using your code in an other OS, what then ? There’s more logic to think about to build your path. That’s not something that you want. Less code is better code for you, because there’s less to maintain 🙂

Path.Combine does a lot of good ! For whatever OS your code might run on, it will build the correct path by using Path.PathSeparator. While building your path, it’s going to check whether or not the first path already has a separator at the end so it won’t duplicate it. While combining the paths, it’s going to check whether or not every paths, starting from the second one you provide, is a root path or not. It’s really easy to use to ! Being that it can accept an infinity of paths, you have no excuse to not use this jewel of .NET!

Path.Combine(firstPath, secondPath, ..., nPath)




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s