Skip to content

Instantly share code, notes, and snippets.

@KyleGobel
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KyleGobel/4b0f2b93014cae7bb52b to your computer and use it in GitHub Desktop.
Save KyleGobel/4b0f2b93014cae7bb52b to your computer and use it in GitHub Desktop.
Mapping Path Extensions for easily getting the correct Paths in .net projects. Idea taken from ServiceStack.
using System;
using System.IO;
/// <summary>
/// Mapping Path Extensions
/// </summary>
/// Gist Url: https://gist.github.com/KyleGobel/4b0f2b93014cae7bb52b
public static class PathUtilities
{
private static string MapAbsolutePath(string relativePath, string appendPartialPathModifier)
{
if (!relativePath.StartsWith("~")) return relativePath;
var localAssembly = (typeof (PathUtilities).Assembly.EscapedCodeBase);
var assemblyPath = Path.GetDirectoryName(new Uri(localAssembly).LocalPath);
var hostDirectoryPath = appendPartialPathModifier != null
? assemblyPath + appendPartialPathModifier
: assemblyPath;
return Path.GetFullPath(relativePath.Replace("~", hostDirectoryPath));
}
/// <summary>
/// Maps path of a file in a self-hosted scenario (assumes static content is copied to /bin/)
/// </summary>
public static string MapAbsolutePath(this string relativePath)
{
return MapAbsolutePath(relativePath, null);
}
/// <summary>
/// Maps path of a file in an Asp.Net hosted scenario (assumes roots is parent of /bin/)
/// </summary>
public static string MapHostAbsolutePath(this string relativePath)
{
return MapAbsolutePath(relativePath, string.Format("{0}..", Path.DirectorySeparatorChar));
}
/// <summary>
/// Maps path of a file in a VS Project (assumes root is 2 levels above /bin/)
/// </summary>
public static string MapProjectPath(this string relativePath)
{
return MapAbsolutePath(relativePath, string.Format("{0}..{0}..", Path.DirectorySeparatorChar));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment