-
Notifications
You must be signed in to change notification settings - Fork 4
/
PathHelper.cs
74 lines (65 loc) · 3.33 KB
/
PathHelper.cs
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
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace PlayListGenerator
{
public static class PathHelper
{
/// <summary>
/// Rebases file with path fromPath to folder with baseDir.
/// </summary>
/// <param name="_fromPath">Full file path (absolute)</param>
/// <param name="_baseDir">Full base directory path (absolute)</param>
/// <returns>Relative path to file in respect of baseDir</returns>
public static String MakeRelative(String _fromPath, String _baseDir)
{
// From https://sourceforge.net/p/syncproj/code/HEAD/tree/syncProj.cs#l735
String pathSep = "\\";
String fromPath = Path.GetFullPath(_fromPath);
String baseDir = Path.GetFullPath(_baseDir); // If folder contains upper folder references, they gets lost here. "c:\test\..\test2" => "c:\test2"
String[] p1 = Regex.Split(fromPath, "[\\\\/]").Where(x => x.Length != 0).ToArray();
String[] p2 = Regex.Split(baseDir, "[\\\\/]").Where(x => x.Length != 0).ToArray();
int i = 0;
for (; i < p1.Length && i < p2.Length; i++)
if (String.Compare(p1[i], p2[i], true) != 0) // Case insensitive match
break;
if (i == 0) // Cannot make relative path, for example if resides on different drive
return fromPath;
String r = String.Join(pathSep, Enumerable.Repeat("..", p2.Length - i).Concat(p1.Skip(i).Take(p1.Length - i)));
return r;
}
/// <summary>
/// On some devices (Android for example), backslash is not recognized as a directory separator.
/// Slash is recognized on Windows and Android...
/// </summary>
/// <param name="aPath">Path that can contains '\' characters</param>
/// <returns>Same path with the characters '\' replaced by '/'</returns>
public static string PathSlashToBackslash(string aPath)
{
return aPath.Replace('\\','/');
}
/// <summary>
/// Separate the directory and the file mask
/// </summary>
/// <param name="aPathAndMask">for example : "d:\foo\*.bar"</param>
/// <returns>for example : return a tuple with "d:\foo" and "*.bar"</returns>
public static Tuple<string, string> ExtractDirectoryAndMask(string aPathAndMask)
{
string directory = aPathAndMask;
string mask = "*.*";
if (directory.Contains('*') || directory.Contains('?') || !Directory.Exists(directory))
{
directory = Path.GetDirectoryName(directory) ?? "";
if (!Directory.Exists(directory) && directory != "")
throw new ExtractDirectoryAndMaskException("Wrong Path : " + aPathAndMask);
mask = aPathAndMask.Remove(0, directory.Length);
if (mask.StartsWith(Path.DirectorySeparatorChar.ToString()))
mask = mask.Remove(0, 1);
}
if (!(mask.Contains('*') || mask.Contains('?')))
throw new ExtractDirectoryAndMaskException("Wrong Mask : " + aPathAndMask);
return new Tuple<string, string>(directory, mask);
}
}
}