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)