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
2009-06-07 11:04:33
+10
A:
System.IO.Path.Combine() is what you need.
Path.Combine(path1, path2);
Cameron MacFarland
2009-06-07 11:05:24
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
2009-06-07 12:01:52
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
2009-06-07 12:08:00
Also, if you're on Mono, this might not work as the path separation character is a "/", not "\".
Jeremy McGee
2009-06-07 12:09:38
@Geo: Ta, Yep that makes sense. @Jeremy: Thanks for confirming my plaform-portability suspicion.
corlettk
2009-06-07 12:29:15