Struct LocalPath
- Namespace
- TruePath
- Assembly
- TruePath.dll
A path pointing to a place in the local file system.
It may be either absolute or relative.
An empty path designates the current directory. Normalizing an empty string, . or
a/.. produces it, and so does Parent of a relative path consisting of a single segment
(e.g. the parent of foo).
Always stored in a normalized form. Read the documentation on Normalize(string) to know what form of normalization the path uses.
public readonly struct LocalPath : IEquatable<LocalPath>, IComparable<LocalPath>, IPath, IPath<LocalPath>
- Implements
- Inherited Members
- Extension Methods
Remarks
Uninitialized values of this structure (e.g. default(LocalPath)) will throw
NullReferenceException from many of the APIs.
Constructors
LocalPath(string)
A path pointing to a place in the local file system.
It may be either absolute or relative.
An empty path designates the current directory. Normalizing an empty string, . or
a/.. produces it, and so does Parent of a relative path consisting of a single segment
(e.g. the parent of foo).
Always stored in a normalized form. Read the documentation on Normalize(string) to know what form of normalization the path uses.
public LocalPath(string value)
Parameters
valuestring
Remarks
Uninitialized values of this structure (e.g. default(LocalPath)) will throw
NullReferenceException from many of the APIs.
LocalPath(AbsolutePath)
Converts an AbsolutePath to a LocalPath.
public LocalPath(AbsolutePath path)
Parameters
pathAbsolutePath
Fields
PlatformDefaultComparer
Provides a default comparer for comparing file paths, aware of the current platform.
On Windows, macOS, iOS and tvOS, this will perform case-insensitive string comparison, since the file systems are case-insensitive on these operating systems by default.
On Linux, the comparison will be case-sensitive.
public static readonly IPathComparer<LocalPath> PlatformDefaultComparer
Field Value
Remarks
Note that this comparison does not guarantee correctness: in practice, on any platform to control case-sensitiveness of either the whole file system or a part of it. This class does not take this into account, having a benefit of no accessing the file system for any of the comparisons.
StrictStringComparer
A strict comparer for comparing file paths using ordinal, case-sensitive comparison of the underlying path strings.
public static readonly IPathComparer<LocalPath> StrictStringComparer
Field Value
Properties
FileName
The name of this path's last component.
public string FileName { get; }
Property Value
IsAbsolute
Checks whether the path is absolute, i.e. it is fully qualified, and doesn't depend on the current directory, or current directory on any drive (Windows-specific).
On Windows, this requires a drive letter and a root directory: C:\Windows is absolute, while
\Windows and C:Windows are not.
public bool IsAbsolute { get; }
Property Value
Remarks
Kind
Determines the kind of this path. See PathKind for the details.
public PathKind Kind { get; }
Property Value
Parent
The parent of this path. Will be null for a rooted absolute path. For a relative path, will always
resolve to its parent directory — by either removing directories from the end of the path, or appending
..
to the end.
public LocalPath? Parent { get; }
Property Value
Remarks
The parent of a relative path consisting of a single segment is the empty path (the current
directory), not null. Similarly, on Windows, the parent of a bare drive (C:, the
current directory of that drive) is C:...
PathRoot
Gets the root of this path, if it can be determined without resolving against the current directory.
-
For an absolute path, this is its root:
C:\forC:\fooon Windows,/for/fooon Unix. -
For a drive-relative path on Windows (e.g.
C:fooorC:), this is the root of that drive (C:\). -
For a relative path, or a path rooted without a drive letter on Windows (e.g.
\foo), this is null.
public AbsolutePath? PathRoot { get; }
Property Value
Value
The normalized path string.
public string Value { get; }
Property Value
Methods
CompareTo(LocalPath)
Compares the current LocalPath instance with another LocalPath instance, using the default platform-aware comparison rules provided by TruePath.Comparers.PlatformDefaultPathComparer<TPath>.
public int CompareTo(LocalPath other)
Parameters
Returns
- int
A signed integer that indicates the relative order of the compared objects.
Value Meaning Less than zero The current instance precedes otherin the sort order.Zero The current instance occurs in the same position in the sort order as other.Greater than zero The current instance follows otherin the sort order.
Create(string)
Creates a new LocalPath instance from the specified string value.
public static LocalPath Create(string value)
Parameters
valuestringThe string representation of the path to create.
Returns
Equals(object?)
Compares the path with another.
public override bool Equals(object? obj)
Parameters
objobject
Returns
Remarks
Uses PlatformDefaultComparer for platform-default case sensitivity.
Equals(LocalPath)
Compares the path with another.
public bool Equals(LocalPath other)
Parameters
otherLocalPath
Returns
Remarks
Uses PlatformDefaultComparer for platform-default case sensitivity.
Equals(LocalPath, IEqualityComparer<LocalPath>)
Determines whether the specified LocalPath is equal to the current LocalPath using the specified string comparer.
public bool Equals(LocalPath other, IEqualityComparer<LocalPath> comparer)
Parameters
otherLocalPathcomparerIEqualityComparer<LocalPath>The comparer to use for comparing the paths.
Returns
- bool
true if the specified LocalPath is equal to the current LocalPath using the specified string comparer; otherwise, false.
GetHashCode()
public override int GetHashCode()
Returns
IsPrefixOf(LocalPath)
public bool IsPrefixOf(LocalPath other)
Parameters
otherLocalPath
Returns
Remarks
Checks for a non-strict prefix: if the paths are equal, then they are still considered prefixes of each other, so every path is a prefix of itself.
RelativeTo(LocalPath)
Calculates the relative path from a base path to this path.
public LocalPath RelativeTo(LocalPath basePath)
Parameters
basePathLocalPathThe base path from which to calculate the relative path.
Returns
- LocalPath
The relative path from the base path to this path, or the resolved absolute path to this path if the paths have different roots.
Remarks
If either path is not absolute, it is first resolved against the current directory, the same way
ResolveToCurrentDirectory() does, so the result may depend on the current directory (or a
drive-specific current directory on Windows). Two relative paths are resolved against the same directory, so
a/b relative to a is b. An empty path designates the current directory.
If the resolved paths have different roots (on Windows, e.g. paths on different drives), there's no relative
path between them, and the resolved absolute path to this path is returned instead: D:\x
relative to C:\y is D:\x. On Unix, all paths share the same root, so this never happens.
ResolveToCurrentDirectory()
Resolves this path to an absolute path based on the current working directory.
public AbsolutePath ResolveToCurrentDirectory()
Returns
- AbsolutePath
An AbsolutePath that represents this path resolved against the current working directory.
Remarks
Note that if this path is already absolute, it will just transform to AbsolutePath. The current directory won't matter for such a case.
On Windows, a path rooted without a drive letter (\x) is resolved against the drive of the current
directory, and a path relative to the current directory of a drive (D:x) is resolved against the
current directory of that drive, as tracked by the process (see GetFullPath(string)), or
against the root of that drive if the process doesn't track one.
This is the same as AbsolutePath.CurrentWorkingDirectory / this (see
operator /(AbsolutePath, LocalPath)), so the result is always absolute.
StartsWith(LocalPath)
Determines whether the current path starts with the specified path.
public bool StartsWith(LocalPath other)
Parameters
otherLocalPathThe path to compare to the current path.
Returns
Remarks
Checks for a non-strict prefix: if the paths are equal, then each still starts with the other, so every path starts with itself.
ToString()
public override string ToString()
Returns
- string
The normalized path string contained in this object.
Operators
operator /(LocalPath, string)
Appends another path to this one.
public static LocalPath operator /(LocalPath basePath, string b)
Parameters
Returns
- LocalPath
The combined path, normalized.
Remarks
The result designates the same location as changing the current directory first to
basePath, and then to b: a / b means the same as
cd /d a && cd /d b on Windows, or cd a && cd b on Unix. A base path without a
drive letter is considered to be on a different drive than any drive letter in b.
Depending on the Kind of b:
-
Absolute (
D:\x,/x):breplaces the base path. -
Relative (
x):bis appended after a separator, soC:\base / xisC:\base\x. No separator is added after a bare drive:C: / xisC:x. Appending an empty path returns the base path unchanged, and appending to an empty path returnsb. -
DriveRootRelative (
\x): the drive of the base path is kept, if there is one.C:\base / \xandC:base / \xare bothC:\x, whilebase / \xis\x. -
DriveCurrentDirectoryRelative (
C:x): if the base path is on the same drive, the rest ofbis appended as a relative path, soC:\base / C:xisC:\base\x. Otherwise,breplaces the base path:C:\base / D:xisD:x, andbase / C:xisC:x. Drive letters are compared case-insensitively.
This is the algorithm of C++'s std::filesystem::path::operator/, except that the result is normalized
(e.g. C:\base / "" gets no trailing separator after normalization).
Important: this operator differs from Combine(string, string), which returns its second argument if that is rooted, and otherwise joins the arguments with a separator:
C:\base / \xisC:\x, whilePath.Combinereturns\x;C:\base / C:xisC:\base\x, whilePath.CombinereturnsC:x;-
C: / xisC:x, whilePath.CombinereturnsC:\xon .NET 10 (.NET Framework 4.8.1, though, will returnC:x); - the result is normalized.
On Unix, the result is the same as the one of Path.Combine, up to normalization.
- See Also
operator /(LocalPath, LocalPath)
Appends another path to this one.
public static LocalPath operator /(LocalPath basePath, LocalPath b)
Parameters
Returns
- LocalPath
The combined path, normalized.
Remarks
The result designates the same location as changing the current directory first to
basePath, and then to b: a / b means the same as
cd /d a && cd /d b on Windows, or cd a && cd b on Unix. A base path without a
drive letter is considered to be on a different drive than any drive letter in b.
Depending on the Kind of b:
-
Absolute (
D:\x,/x):breplaces the base path. -
Relative (
x):bis appended after a separator, soC:\base / xisC:\base\x. No separator is added after a bare drive:C: / xisC:x. Appending an empty path returns the base path unchanged, and appending to an empty path returnsb. -
DriveRootRelative (
\x): the drive of the base path is kept, if there is one.C:\base / \xandC:base / \xare bothC:\x, whilebase / \xis\x. -
DriveCurrentDirectoryRelative (
C:x): if the base path is on the same drive, the rest ofbis appended as a relative path, soC:\base / C:xisC:\base\x. Otherwise,breplaces the base path:C:\base / D:xisD:x, andbase / C:xisC:x. Drive letters are compared case-insensitively.
This is the algorithm of C++'s std::filesystem::path::operator/, except that the result is normalized
(e.g. C:\base / "" gets no trailing separator after normalization).
Important: this operator differs from Combine(string, string), which returns its second argument if that is rooted, and otherwise joins the arguments with a separator:
C:\base / \xisC:\x, whilePath.Combinereturns\x;C:\base / C:xisC:\base\x, whilePath.CombinereturnsC:x;-
C: / xisC:x, whilePath.CombinereturnsC:\xon .NET 10 (.NET Framework 4.8.1, though, will returnC:x); - the result is normalized.
On Unix, the result is the same as the one of Path.Combine, up to normalization.
- See Also
operator ==(LocalPath, LocalPath)
Compares the path with another.
public static bool operator ==(LocalPath left, LocalPath right)
Parameters
Returns
Remarks
Uses PlatformDefaultComparer for platform-default case sensitivity.
implicit operator LocalPath(AbsolutePath)
Implicitly converts an AbsolutePath to a LocalPath.
public static implicit operator LocalPath(AbsolutePath path)
Parameters
pathAbsolutePath
Returns
Remarks
Note that this conversion doesn't lose any information.
operator !=(LocalPath, LocalPath)
Compares the path with another.
public static bool operator !=(LocalPath left, LocalPath right)
Parameters
Returns
Remarks
Uses PlatformDefaultComparer for platform-default case sensitivity.