-
Notifications
You must be signed in to change notification settings - Fork 1
/
StackCleanerUtilities.cs
64 lines (59 loc) · 2.44 KB
/
StackCleanerUtilities.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
using System;
using System.IO;
namespace StackCleaner;
internal class StackCleanerUtilities
{
internal static bool IsChildOf(string? shorterPath, string longerPath, bool includeSubDirectories = true)
{
if (string.IsNullOrEmpty(shorterPath))
return true;
if (string.IsNullOrEmpty(longerPath))
return false;
DirectoryInfo parent = new DirectoryInfo(shorterPath);
DirectoryInfo child = new DirectoryInfo(longerPath);
return IsChildOf(parent, child, includeSubDirectories);
}
internal static bool IsChildOf(DirectoryInfo shorterPath, DirectoryInfo longerPath, bool includeSubDirectories = true)
{
string shortFullname = shorterPath.FullName;
if (!includeSubDirectories)
return longerPath.Parent != null && longerPath.Parent.FullName.Equals(shortFullname, StringComparison.Ordinal);
while (longerPath.Parent != null)
{
if (longerPath.Parent.FullName.Equals(shortFullname, StringComparison.Ordinal))
return true;
longerPath = longerPath.Parent;
}
return false;
}
// https://stackoverflow.com/questions/51179331/is-it-possible-to-use-path-getrelativepath-net-core2-in-winforms-proj-targeti
internal static string GetRelativePath(string relativeTo, string path)
{
if (!IsChildOf(relativeTo, path))
return path;
if (string.IsNullOrEmpty(relativeTo))
{
path = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
if (path.IndexOf(Path.DirectorySeparatorChar) == -1)
path = "." + Path.DirectorySeparatorChar + path;
return path;
}
path = Path.GetFullPath(path);
relativeTo = Path.GetFullPath(relativeTo);
Uri uri = new Uri(relativeTo);
string rel = Uri.UnescapeDataString(uri.MakeRelativeUri(new Uri(path)).ToString()).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
int index = rel.IndexOf(Path.DirectorySeparatorChar);
if (index == -1)
rel = "." + Path.DirectorySeparatorChar + rel;
else
{
if (index != rel.Length - 1)
{
rel = rel.Substring(index + 1);
if (rel.IndexOf(Path.DirectorySeparatorChar) == -1)
rel = "." + Path.DirectorySeparatorChar + rel;
}
}
return rel;
}
}