Last active
August 29, 2015 14:02
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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