Geeks With Blogs
Sean Rhone

Recently at work I had to track down some errors that were being thrown when trying to write out some files. The logs showed the path the files were trying to be written to but they were not in a valid format. So, I fould the line of code that built the path

sPath = Path.Combine("C:\Logging","ClientOne" I removed the variables and put in values.

Looks straigt forward enough until I noticed that the second value had been chaged to "\ClientOne" in the database causing the code to look like

sPath = Path.Combine("C:\Logging","\ClientOne"

Now, I would think that this line would produce C:\Logging\ClientOne but instead in produces "\ClientOne". Doesn't seem like a valid output to me so I fired up Lutz Roeder's Reflectors (http://www.aisto.com/roeder/dotnet/) application and took a look at the Mircosoft mscorlib which contains System.IO.Path.Combine method. As it turns out, if your second parameter contains a "\" or the alternate charactor it returns your second parameter as your combined string. Microsoft states this behavior on the documentation for this method but I'm wondering why anyone thought this would be the desired behavior?

Anyone reading this have idea's on why this works this way? Am I the only one who thinks this is odd?

Posted on Friday, May 9, 2008 8:14 AM | Back to top


Comments on this post: System.IO.Path

# re: System.IO.Path
Requesting Gravatar...
Yeah, I thought it was "interesting" too when I learned of that behavior...

http://coolthingoftheday.blogspot.com/2008/02/pathcombine-be-aware-of-slash-in-second.html

I believe it's meant to follow the convention that when referring to a file by "\greg.doc" you really mean "root:\greg.doc". So Path.Combine is helping by seeing that intended "root" reference and fixing the path for you...

Yeah, I know, I thought it was kind of weird too...

Left by Greg on May 09, 2008 12:59 PM

Your comment:
 (will show your gravatar)


Copyright © Sean Rhone | Powered by: GeeksWithBlogs.net