-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathPathUtil.cs
More file actions
100 lines (82 loc) · 2.81 KB
/
PathUtil.cs
File metadata and controls
100 lines (82 loc) · 2.81 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Diagnostics.Contracts;
using System.IO;
using System.Reflection;
namespace ReClassNET.Util
{
public class PathUtil
{
private static readonly Lazy<string> executablePath = new Lazy<string>(() =>
{
string path = null;
try
{
path = Assembly.GetExecutingAssembly().Location;
}
catch
{
// ignored
}
if (string.IsNullOrEmpty(path))
{
path = Assembly.GetExecutingAssembly().GetName().CodeBase;
path = FileUrlToPath(path);
}
return path;
});
/// <summary>Gets the full pathname of the executable file.</summary>
public static string ExecutablePath => executablePath.Value;
private static readonly Lazy<string> executableFolderPath = new Lazy<string>(() => Path.GetDirectoryName(executablePath.Value));
/// <summary>Gets the full pathname of the executable folder.</summary>
public static string ExecutableFolderPath => executableFolderPath.Value;
private static readonly Lazy<string> temporaryFolderPath = new Lazy<string>(Path.GetTempPath);
/// <summary>Gets the full pathname of the temporary folder.</summary>
/// <remarks>%temp%</remarks>
public static string TemporaryFolderPath => temporaryFolderPath.Value;
private static readonly Lazy<string> settingsFolderPath = new Lazy<string>(() =>
{
string applicationData;
try
{
applicationData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
}
catch (Exception)
{
applicationData = executableFolderPath.Value;
}
string localApplicationData;
try
{
localApplicationData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
}
catch (Exception)
{
localApplicationData = applicationData;
}
return Path.Combine(localApplicationData, Constants.ApplicationName);
});
/// <summary>Gets the full pathname of the settings folder.</summary>
/// <remarks>%localappdata%\ReClass.NET\</remarks>
public static string SettingsFolderPath => settingsFolderPath.Value;
private static readonly Lazy<string> launcherExecutablePath = new Lazy<string>(() =>
{
var path = Path.Combine(Directory.GetParent(ExecutableFolderPath).FullName, Constants.LauncherExecutableName);
return !File.Exists(path) ? null : path;
});
/// <summary>Gets the full pathname of the launcher executable.</summary>
public static string LauncherExecutablePath => launcherExecutablePath.Value;
/// <summary>Converts a file url to a normal path.</summary>
/// <param name="url">URL of the file.</param>
/// <returns>The path part of the URL.</returns>
public static string FileUrlToPath(string url)
{
Contract.Requires(url != null);
if (url.StartsWith("file:///", StringComparison.OrdinalIgnoreCase))
{
url = url.Substring(8);
}
url = url.Replace('/', Path.DirectorySeparatorChar);
return url;
}
}
}