The System.IO namespace has a Path class, which is used to manipulate file paths. Things like 'GetFileNameWithoutExtension' are very, very useful. Some things are a little counter intuitive, such as Path.GetDirectory walking up the directory tree if the string you have is a directory already, but overall, it saves a lot of work.
One of the things I use the most, is Path.Combine, which takes two fragments and merges them to make a path. In the past, I'd be checking if one string had a trailing slash, if the other had a leading slash, etc. Path.Combine takes care of that for you. Right ? Not quite.
There's a couple of quirks here. To illustrate, the following table has three columns. The first two are the arguments passed into Path.Combine, the third is the result.
| c:\path\ | dir\file.txt | c:\path\dir\file.txt |
| c:\path\ | \dir\file.txt | \dir\file.txt |
| c:\path | dir\file.txt | c:\path\dir\file.txt |
| c:\path | \dir\file.txt | \dir\file.txt |
| c: | dir\file.txt | c:dir\file.txt |
| c: | \dir\file.txt | \dir\file.txt |
The first thing to notice, is that if the second string starts with a \, then you get the second string back verbatim. This is the issue that hit me in the past. I assumed that this method existed so no matter what slashes happened to be in the two strings, they would get joined into a single path. As you can see, this is not so. Now, I assume there's a specific case for which this behaviour is desirable, but it's not the most obvious one to me, and if there's a reason for it, surely the method could have an overload, or better yet, a method called something in line with the reasoning for not combining these two strings could exist ( a method called Combine, is one I call to combine strings, not to SOMETIMES Combine them ).
The second one is more interesting. If my first string is a drive letter, with no slash in it, then no slash is added. I just did a test, I have a file called c:\procs.txt. File.Exists (@"c:procs.txt") returns false, File.Exists(@"c:\procs.txt") returns true. So, it seems to me that the slash is needed, but Path.Combine does not add it.
Overall, this method is basically broken as far as I am concerned, and I have rolled my own version to use instead. It basically makes sure the first string has a \ at the end, the second doesn't have one at the start, then calls Path.Combine, just out of spite ( given that at this point I could just concatenate the two strings and be done with it )