Friday, March 28, 2008

 

Note to self: combining base path and relative path to an absolute path in C#

Just to keep myself from figuring this out every time again. ;)

When building a URL from a base URL (say: http://www.teuntostring.net/blog/2008) and a relative path (say: ../../blog/index.html), the .NET framework supports us nicely with the Uri class. Just say:

This works nicely, but for file system paths ("c:\Temp\myfile.htm"), the Uri class isnot suitable. You can of course transcribe all file paths to Uri form (file:///c:/...), but the resulting Uri cannot be used to open the file. It would be nice to have a way to combine file paths in the way we can with URLs. At first I thought that the Path.Combine method would help me out, but alas: Using Reflector on the mscorlib led me to the Path.GetFullPath method. It can normalize a path with relative parts into a clean absolute path. So the way appears to be: I wonder if an easier and cleaner way exists. This solution doesn't feel like it was meant to be like this.

This page is powered by Blogger. Isn't yours?