forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathUtils.cs
More file actions
84 lines (72 loc) · 3.13 KB
/
PathUtils.cs
File metadata and controls
84 lines (72 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.IO;
using System.Text;
namespace ServiceStack.Common.Utils
{
public static class PathUtils
{
public static string MapAbsolutePath(string relativePath, string appendPartialPathModifier)
{
#if !SILVERLIGHT
if (relativePath.StartsWith("~"))
{
var assemblyDirectoryPath = Path.GetDirectoryName(new Uri(typeof(PathUtils).Assembly.EscapedCodeBase).LocalPath);
// Escape the assembly bin directory to the hostname directory
var hostDirectoryPath = appendPartialPathModifier != null
? assemblyDirectoryPath + appendPartialPathModifier
: assemblyDirectoryPath;
return Path.GetFullPath(relativePath.Replace("~", hostDirectoryPath));
}
#endif
return relativePath;
}
/// <summary>
/// Maps the path of a file in the context of a VS project
/// </summary>
/// <param name="relativePath">the relative path</param>
/// <returns>the absolute path</returns>
/// <remarks>Assumes static content is two directories above the /bin/ directory,
/// eg. in a unit test scenario the assembly would be in /bin/Debug/.</remarks>
public static string MapProjectPath(this string relativePath)
{
var mapPath = MapAbsolutePath(relativePath, string.Format("{0}..{0}..", Path.DirectorySeparatorChar));
return mapPath;
}
/// <summary>
/// Maps the path of a file in a self-hosted scenario
/// </summary>
/// <param name="relativePath">the relative path</param>
/// <returns>the absolute path</returns>
/// <remarks>Assumes static content is copied to /bin/ folder with the assemblies</remarks>
public static string MapAbsolutePath(this string relativePath)
{
var mapPath = MapAbsolutePath(relativePath, null);
return mapPath;
}
/// <summary>
/// Maps the path of a file in an Asp.Net hosted scenario
/// </summary>
/// <param name="relativePath">the relative path</param>
/// <returns>the absolute path</returns>
/// <remarks>Assumes static content is in the parent folder of the /bin/ directory</remarks>
public static string MapHostAbsolutePath(this string relativePath)
{
var mapPath = MapAbsolutePath(relativePath, string.Format("{0}..", Path.DirectorySeparatorChar));
return mapPath;
}
internal static string CombinePaths(StringBuilder sb, params string[] paths)
{
foreach (var path in paths)
{
if (sb.Length > 0)
sb.Append("/");
sb.Append(path.TrimStart('/', '\\'));
}
return sb.ToString();
}
public static string CombinePaths(params string[] paths)
{
return CombinePaths(new StringBuilder(), paths);
}
}
}