-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathUtility.cs
More file actions
51 lines (49 loc) · 1.76 KB
/
PathUtility.cs
File metadata and controls
51 lines (49 loc) · 1.76 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
/*************************************************************************************************
*
* THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
* PARTICULAR PURPOSE.
*
*************************************************************************************************/
#if DEBUG
using System.Diagnostics;
#endif
using System;
using System.IO;
using System.Web.Hosting;
namespace Figaro.Configuration
{
/// <summary>
/// Used to resolve the file paths containing environment variables.
/// </summary>
internal static class PathUtility
{
private const char EnvSplit = '%';
/// <summary>
/// Resolve paths possibly containing environment variables.
/// </summary>
/// <param name="path">The directory path to resolve.</param>
/// <returns>A resolved path.</returns>
public static string ResolvePath(string path)
{
var ret = path;
var envs = path.Split(new[] { EnvSplit });
foreach (var env in envs)
{
var envValue = EnvSplit + env + EnvSplit;
if (string.IsNullOrEmpty(env)) continue;
var envPaths = Environment.ExpandEnvironmentVariables(envValue).Split(new[] { Path.PathSeparator });
ret = ret.Replace(envValue, envPaths[0]);
}
if (HostingEnvironment.IsHosted)
{
ret = HostingEnvironment.MapPath(ret);
}
#if DEBUG
Debug.WriteLine(string.Format("resolving '{0}' to '{1}'... ", path, ret), "Configuration");
#endif
return ret;
}
}
}