Incorrect Results With Path.Combine - Hilton Giesenow's Jumbled Mind

Hilton Giesenow's Jumbled Mind

the madness that is...

News

This is my little spot in cyberspace where you will find a collection of random (but mostly software-related) thoughts and ideas that are frightening in their shining brilliance (or something like that ;->).
 
Please enjoy your stay and feel free to Contact Me.
 
Microsoft MVP

.Net Links

BlogRoll

Misc. Links

Syndication

Incorrect Results With Path.Combine

A good while ago I posted about using Path.Combine to combine two paths into 1 final path. For instance, if you have a folder portion you've extracted from somewhere (maybe a root path for something you're storing in the config file, like 'c:\temp') and a final filename (like 'myfile.txt', Path.Combine("c:\temp", "myfile.txt") will join the two for a resulting filename: c:\temp\myfile.txt

There are a few interesting combinations of what will and won't combine correctly, and the MSDN page for Path.Combine explains some of these. There's one condition it won't cater for properly though - if the 2nd parameter contains a leading '\', for instance '\myfile.txt', the final result will ignore the first parameter. The output of Path.Combine("c:\temp", "\myfile.txt") is \myfile.txt'. This is not the case if the 1st parameter contains a trailing '\'. See the following for more info:

string part1 = @"c:\temp";
string part2 = @"assembly1.dll";

(1) Console.WriteLine(Path.Combine(part1, part2));
(2) Console.WriteLine(Path.Combine(part1 + @"\", part2));
(3) Console.WriteLine(Path.Combine(part1 + @"\", @"\" + part2));
(4) Console.WriteLine(Path.Combine(part1, @"\" + part2));

The output of this is

(1) c:\temp\assembly1.dll
(2) c:\temp\assembly1.dll
(3) \assembly1.dll
(4) \assembly1.dll

So the moral is - check your 2nd path for a leading '\'.

Comments

Hilton Giesenow's Jumbled Mind said:

I was trying to load a file from a supplied path and name, and I was concatenating strings. I forgot

# February 19, 2008 8:33 AM

Aaron Jensen said:

I wouldn't call this incorrect... \foo.txt and /foo.txt have always meant root\foo.txt. It makes perfect sense that this would behave like this... or throw an exception.

# February 19, 2008 8:45 AM

hiltong said:

Fair enough. I didn't mean it was wrong, as I suspect it is by design. My point was that it's important to be aware of this behaviour otherwise you might run into trouble. Thanks for the info though.

- H

# February 19, 2008 8:53 AM

Pablo Alarcon Garcia said:

I also found this a while ago. Boy, I've been wrong on this for so long, telling everybody to use Path.Combine instead of concatenating strings...

# March 14, 2008 11:55 AM

hiltong said:

Well, I think it's safer to use Path.Combine, but like everything else - you need to know it's limitations

# March 14, 2008 1:07 PM

Path combining problem in c# - SURESHKUMAR.NET FORUMS said:

Pingback from  Path combining problem in c# - SURESHKUMAR.NET FORUMS

# August 21, 2008 3:39 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: