EasySign BETA
Digital Signing Tool
Loading...
Searching...
No Matches
OSPath.cs
Go to the documentation of this file.
1using static System.IO.Path;
2
4{
8 public class OSPath
9 {
13 public static readonly OSPath Empty = "";
14
18 public static bool IsWindows => DirectorySeparatorChar == '\\';
19
24 public OSPath(string text) => Text = text.Trim();
25
30 public static implicit operator OSPath(string text) => new OSPath(text);
31
36 public static implicit operator string(OSPath path) => path.Normalized;
37
39 public override string ToString() => Normalized;
40
44 protected string Text { get; }
45
49 public string Normalized => IsWindows ? Windows : Unix;
50
54 public string Windows => Text.Replace('/', '\\');
55
59 public string Unix => Simplified.Text.Replace('\\', '/');
60
64 public OSPath Relative => Simplified.Text.TrimStart('/', '\\');
65
69 public bool IsAbsolute => IsRooted || HasVolume;
70
74 public bool IsRooted => Text.Length >= 1 && (Text[0] == '/' || Text[0] == '\\');
75
79 public bool HasVolume => Text.Length >= 2 && Text[1] == ':';
80
84 public OSPath Simplified => HasVolume ? Text.Substring(2) : Text;
85
90 {
91 get
92 {
93 var parent = GetDirectoryName(Text);
94
95 if (parent == null)
96 {
97 var root = GetPathRoot(Text);
98
99 if (root == null)
100 {
101 return Empty;
102 }
103
104 return root;
105 }
106
107 return parent;
108 }
109 }
110
116 public bool Contains(OSPath path) => Normalized.StartsWith(path);
117
124 public static OSPath operator +(OSPath left, OSPath right) =>
125 new OSPath(Combine(left, right.Relative));
126
133 public static OSPath operator -(OSPath left, OSPath right) =>
134 left.Contains(right)
135 ? new OSPath(left.Normalized.Substring(right.Normalized.Length)).Relative
136 : left;
137 }
138}
Represents an operating system path and provides methods for path conversion and manipulation.
Definition OSPath.cs:9
string Normalized
Gets the normalized path based on the current operating system.
Definition OSPath.cs:49
static readonly OSPath Empty
Represents an empty OSPath.
Definition OSPath.cs:13
string Unix
Gets the Unix-style path.
Definition OSPath.cs:59
OSPath Parent
Gets the parent directory of the path.
Definition OSPath.cs:90
OSPath Simplified
Gets the rooted path without the drive letter.
Definition OSPath.cs:84
static OSPath operator-(OSPath left, OSPath right)
Removes the specified path from the current path.
OSPath Relative
Gets the path without the root or drive letter.
Definition OSPath.cs:64
bool HasVolume
Gets a value indicating whether the path has a drive letter.
Definition OSPath.cs:79
OSPath(string text)
Initializes a new instance of the OSPath class with the specified text.
static OSPath operator+(OSPath left, OSPath right)
Concatenates two paths.
bool IsAbsolute
Gets a value indicating whether the path is absolute.
Definition OSPath.cs:69
bool Contains(OSPath path)
Determines whether the current path contains the specified path.
static bool IsWindows
Gets a value indicating whether the current operating system is Windows.
Definition OSPath.cs:18
bool IsRooted
Gets a value indicating whether the path is rooted.
Definition OSPath.cs:74
string Text
Gets the original path text.
Definition OSPath.cs:44
string Windows
Gets the Windows-style path.
Definition OSPath.cs:54