Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official ASP.NET Blog

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
Add to Technorati Favorites


ASP Insider Follow Me On Twitter
Put Your Path Together with Path.Combine

At least in my history of programming I have always concatenated paths as I build them in my code. A good example of this is deciding where to store uploaded photos in a photo album. There are really two paths that need to be managed, the physical path on the server and the direct URL to the photo. It always seemed to break at one point or another with my sites, typically at some random time when I was extremely busy with another project and working against a deadline.

I think I remember seeing the Path.Combine function a few years ago, but what it does did not register in my normal coding mind set. As I wrote my book last year I kept seeing it used in the framework code as I analyzed it in Reflector. So I started investigating just how I could use it and now make it a routine GoTo method.

The Combine method is a static or shared function that returns a full path, properly combining the two strings passed into a valid path. The really nice thing is that it will do the work for us to create valid physical, shared or URL paths. This is very important because now I really do not need to manage several methods in my head to take care of each scenario, making my life much easier.

Since starting to use the path.combined method I have found that my code is much more consistent and I have fewer errors when it comes to working with file paths.  One neat trick I have figured out is that I can combine directory structures and then combine the file name at the end of the string.  On a related topic I have also discovered that I can use the file not exist method to verify that the actual file exist, again making my code more robust and error free. Another important property that I have begun using is the Request.PhysicalAppliccationPath property, because it returns that actual physical path to the site is stored on the server.

An Example using Path.Combine from my upcoming httpHandler WROX BLOX

'First run the supplied filename through a URL decoder to change any URL
'characters back to their normal selves.
sDocument = HttpUtility.UrlDecode( _
    Path.GetFileName(context.Request.Url.ToString))
'Now build a full physical path to the file on the server.
sDocument = Path.Combine(Path.Combine( _
    context.Request.PhysicalApplicationPath, "PDF"), sDocument)

Posted: Sunday, March 30, 2008 12:35 PM

by Chris Love
Filed under: , ,

Comments

Oliver said:

Well, I'm not as happy as you seem to be with the Path.Combine method because e.g. Path.Combine("c:\\projects", "/relative/url/to/pic.jpg") returns just "/relative/url/to/pic.jpg", which is not, what I would expect. And even omitting the leading slash in the second part of the path, I just get an ugly "c:\\projects/relative/url/to/pic.jpg" which works but seems not a very clean approach. Cheers, Oliver
# February 9, 2010 7:17 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS