Here’s a nice and simple path utility that I’ve needed in a number of applications: I need to find a relative path based on a base path. So if I’m working in a folder called c:\temp\templates\ and I want to find a relative path for c:\temp\templates\subdir\test.txt I want to receive back subdir\test.txt. Or if I pass c:\ I want to get back ..\..\ – in other words always return a non-hardcoded path based on some other known directory.
I’ve had a routine in my library that does this via some lengthy string parsing routines, but ran into some Uri processing today that made me realize that this code could be greatly simplified by using the System.Uri class instead. Here’s the simple static method:
/// <summary>
/// Returns a relative path string from a full path based on a base path
/// provided.
/// </summary>
/// <param name="fullPath">The path to convert. Can be either a file or a directory</param>
/// <param name="basePath">The base path on which relative processing is based. Should be a directory.</param>
/// <returns>
/// String of the relative path.
///
/// Examples of returned values:
/// test.txt, ..\test.txt, ..\..\..\test.txt, ., .., subdir\test.txt
/// </returns>
public static string GetRelativePath(string fullPath, string basePath )
{
// Require trailing backslash for path
if (!basePath.EndsWith("\\"))
basePath += "\\";
Uri baseUri = new Uri(basePath);
Uri fullUri = new Uri(fullPath);
Uri relativeUri = baseUri.MakeRelativeUri(fullUri);
// Uri's use forward slashes so convert back to backward slashes
return relativeUri.ToString().Replace("/", "\\");
}
You can then call it like this:
string relPath = FileUtils.GetRelativePath("c:\temp\templates\subdir\test.txt","c:\temp\templates")
It’s not exactly rocket science but it’s useful in many scenarios where you’re working with files based on an application base directory. I often forget that the URI class can basically work of file paths as well to slice and dice the path portions which is a good thing to remember at times. The only thing to remember if you do use it that it turns file system backwards slashes into forward slashes so that has to be reversed out.
I needed this because right now I’m working on a templating solution (using the Razor Engine) where templates live in a base directory and are supplied as relative paths to that base directory. Resolving these relative paths both ways is important in order to properly check for existance of files and their change status in this case.
Not the kind of thing you use every day, but useful to remember.
Other Posts you might also like