forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathHelpers.cs
More file actions
121 lines (102 loc) · 2.98 KB
/
PathHelpers.cs
File metadata and controls
121 lines (102 loc) · 2.98 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Linq;
using System.Text;
using JavaScriptEngineSwitcher.Core.Resources;
namespace JavaScriptEngineSwitcher.Core.Helpers
{
/// <summary>
/// Path helpers
/// </summary>
public static class PathHelpers
{
/// <summary>
/// Converts a back slashes to forward slashes
/// </summary>
/// <param name="path">Path with back slashes</param>
/// <returns>Path with forward slashes</returns>
public static string ProcessBackSlashes(string path)
{
if (path == null)
{
throw new ArgumentNullException("path",
string.Format(Strings.Common_ArgumentIsNull, "path"));
}
if (string.IsNullOrWhiteSpace(path))
{
return path;
}
string result = path.Replace('\\', '/');
return result;
}
/// <summary>
/// Determines whether the path contains the specified directory
/// </summary>
/// <param name="path">Path</param>
/// <param name="directoryName">Name of directory</param>
/// <returns>true if the path contains an directory with the specified name; otherwise, false</returns>
public static bool ContainsDirectory(string path, string directoryName)
{
if (path == null)
{
throw new ArgumentNullException("path",
string.Format(Strings.Common_ArgumentIsNull, "path"));
}
if (directoryName == null)
{
throw new ArgumentNullException("directoryName",
string.Format(Strings.Common_ArgumentIsNull, "directoryName"));
}
if (string.IsNullOrWhiteSpace(path) || string.IsNullOrWhiteSpace(directoryName))
{
return false;
}
string processedPath = ProcessBackSlashes(path);
string[] pathParts = processedPath.Split('/');
bool result = pathParts.Contains(directoryName, StringComparer.OrdinalIgnoreCase);
return result;
}
/// <summary>
/// Removes a directory from path
/// </summary>
/// <param name="path">Path</param>
/// <param name="directoryName">Name of directory</param>
/// <returns>Path without specified directory</returns>
public static string RemoveDirectoryFromPath(string path, string directoryName)
{
if (path == null)
{
throw new ArgumentNullException("path",
string.Format(Strings.Common_ArgumentIsNull, "path"));
}
if (directoryName == null)
{
throw new ArgumentNullException("directoryName",
string.Format(Strings.Common_ArgumentIsNull, "directoryName"));
}
if (string.IsNullOrWhiteSpace(path))
{
return string.Empty;
}
string processedPath = ProcessBackSlashes(path);
string[] pathParts = processedPath.Split('/');
int pathPartCount = pathParts.Length;
string newPath = string.Empty;
if (pathPartCount > 0)
{
var sb = new StringBuilder();
for (int pathPartIndex = 0; pathPartIndex < pathPartCount; pathPartIndex++)
{
if (pathParts[pathPartIndex].Equals(directoryName, StringComparison.OrdinalIgnoreCase))
{
break;
}
sb.Append(pathParts[pathPartIndex]);
sb.Append("/");
}
newPath = sb.ToString();
sb.Clear();
}
return newPath;
}
}
}