tags:

views:

1310

answers:

3

I searched StackOverflow for the title of this question, but I didn't find anything ... so, how do I join two file paths ?

+23  A: 

You have to use Path.Combine() as in the example below:

string basePath = @"c:\temp";
string filePath = "test.txt";
string combinedPath = Path.Combine(basePath, filePath); 
// produces c:\temp\test.txt
Jose Basilio
+10  A: 

System.IO.Path.Combine() is what you need.

Path.Combine(path1, path2);
Cameron MacFarland
A: 

Am I just being old fashioned (aka silly) here...

string basePath = @"c:\temp";
string filePath = "test.txt";
string combinedPath = basePath+"\\"+filePath; 
// produces c:\temp\test.txt

I've been doing it that way for a good many years, without any (AFAIK) problems... What value does Path.Combine add? Is it platform sensitive? But, but, but: That doesn't hold with my conception of a Microsoft product ;-)

Cheers. Keith.

corlettk
using your version, you would also need to check and see if the first string ends with a "\\" or if the second starts with "\\".
Geo
Also, if you're on Mono, this might not work as the path separation character is a "/", not "\".
Jeremy McGee
@Geo: Ta, Yep that makes sense. @Jeremy: Thanks for confirming my plaform-portability suspicion.
corlettk