Table of Contents

Struct AbsolutePath

Namespace
TruePath
Assembly
TruePath.dll

This is a path on the local system that's guaranteed to be absolute: that is, path that is rooted and has a disk letter (on Windows).

public readonly struct AbsolutePath : IEquatable<AbsolutePath>, IComparable<AbsolutePath>, IPath, IPath<AbsolutePath>
Implements
Inherited Members
Extension Methods

Remarks

For a path that's not guaranteed to be absolute, use the LocalPath type.

Uninitialized values of this structure (e.g. default(AbsolutePath)) will throw NullReferenceException from many of the APIs.

Constructors

AbsolutePath(string)

Creates an AbsolutePath instance by normalizing the path from the passed string according to the rules stated in LocalPath.

public AbsolutePath(string value)

Parameters

value string

Path string to normalize.

Exceptions

ArgumentException

Thrown if the passed string does not represent an absolute path.

AbsolutePath(LocalPath)

Creates an AbsolutePath instance by converting a localPath object.

public AbsolutePath(LocalPath localPath)

Parameters

localPath LocalPath

Exceptions

ArgumentException

Thrown if the passed path is not absolute.

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<AbsolutePath> PlatformDefaultComparer

Field Value

IPathComparer<AbsolutePath>

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<AbsolutePath> StrictStringComparer

Field Value

IPathComparer<AbsolutePath>

Properties

CurrentWorkingDirectory

Gets or sets the current working directory as an AbsolutePath instance.

public static AbsolutePath CurrentWorkingDirectory { get; set; }

Property Value

AbsolutePath

The current working directory.

FileName

The name of this path's last component.

public string FileName { get; }

Property Value

string

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 AbsolutePath? Parent { get; }

Property Value

AbsolutePath?

PathRoot

Gets the root of this path: e.g. C:</code> on Windows or / on Unix.

public AbsolutePath PathRoot { get; }

Property Value

AbsolutePath

Value

The normalized path string.

public string Value { get; }

Property Value

string

Methods

Canonicalize()

Corrects the file name case on case-insensitive file systems, resolves symlinks.

public AbsolutePath Canonicalize()

Returns

AbsolutePath

CompareTo(AbsolutePath)

Compares the current AbsolutePath instance with another AbsolutePath instance, using the default platform-aware comparison rules provided by TruePath.Comparers.PlatformDefaultPathComparer<TPath>.

public int CompareTo(AbsolutePath other)

Parameters

other AbsolutePath

The AbsolutePath instance to compare with the current instance.

Returns

int

A signed integer that indicates the relative order of the compared objects.

ValueMeaning
Less than zeroThe current instance precedes other in the sort order.
ZeroThe current instance occurs in the same position in the sort order as other.
Greater than zeroThe current instance follows other in the sort order.

Create(string)

Creates a new path instance of type AbsolutePath from the specified string value.

public static AbsolutePath Create(string value)

Parameters

value string

The string representation of the path to create.

Returns

AbsolutePath

A new instance of AbsolutePath representing the specified path.

Equals(object?)

Compares the path with another.

public override bool Equals(object? obj)

Parameters

obj object

Returns

bool

Remarks

Uses PlatformDefaultComparer for platform-default case sensitivity.

Equals(AbsolutePath)

Compares the path with another.

public bool Equals(AbsolutePath other)

Parameters

other AbsolutePath

Returns

bool

Remarks

Uses PlatformDefaultComparer for platform-default case sensitivity.

Equals(AbsolutePath, IEqualityComparer<AbsolutePath>)

Determines whether the specified AbsolutePath is equal to the current AbsolutePath using the specified comparer.

public bool Equals(AbsolutePath other, IEqualityComparer<AbsolutePath> comparer)

Parameters

other AbsolutePath

The AbsolutePath to compare with the current AbsolutePath.

comparer IEqualityComparer<AbsolutePath>

The comparer to use for comparing the paths. For example, pass PlatformDefaultComparer or StrictStringComparer.

Returns

bool

true if the specified AbsolutePath is equal to the current AbsolutePath using the specified comparer; otherwise, false.

GetHashCode()

public override int GetHashCode()

Returns

int

IsPrefixOf(AbsolutePath)

public bool IsPrefixOf(AbsolutePath other)

Parameters

other AbsolutePath

Returns

bool

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.

ReadKind()

Determines the type of the file system entry (file, directory, symlink, or junction) for the given path.

public FileEntryKind? ReadKind()

Returns

FileEntryKind?

A FileEntryKind enumeration value representing the type of the file system entry, or null if the entry does not exist.

Remarks

This method checks if the specified path represents a file, directory, symbolic link, or junction. On Windows, it uses IsJunction(string) to identify junctions and checks for the ReparsePoint flag to identify symbolic links.

RelativeTo(AbsolutePath)

Calculates the relative path from a base path to this path.

public LocalPath RelativeTo(AbsolutePath basePath)

Parameters

basePath AbsolutePath

The base path from which to calculate the relative path.

Returns

LocalPath

The relative path from the base path to this path, or this path itself if the paths have different roots.

Remarks

If the paths have different roots (on Windows, e.g. paths on different drives), there's no relative path between them, and this path is returned unchanged: D:\x relative to C:\y is D:\x. On Unix, all paths share the same root, so this never happens.

StartsWith(AbsolutePath)

Determines whether the current path starts with the specified path.

public bool StartsWith(AbsolutePath other)

Parameters

other AbsolutePath

The path to compare to the current path.

Returns

bool

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 /(AbsolutePath, string)

Works the same way as operator /(LocalPath, LocalPath) (read its documentation for the details, including the differences from Combine(string, string)), except that the result is always absolute: a path relative to the current directory of another drive gets resolved (see the 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. This is the algorithm of C++'s std::filesystem::path::operator/, except that the result is normalized, and that a path relative to the current directory of another drive gets resolved.

public static AbsolutePath operator /(AbsolutePath basePath, string b)

Parameters

basePath AbsolutePath
b string

Returns

AbsolutePath

Remarks

On Windows, a path relative to the current directory of another drive 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. E.g. C:\base / D:x is D:\x if the current directory of drive D: is its root. This is the only case when the result depends on the state of the process: operator /(LocalPath, LocalPath) returns D:x here.

On the same drive, such a path is resolved against the base path: C:\base / C:x is C:\base\x. A path rooted without a drive letter keeps the drive of the base path: C:\base / \x is C:\x.

See Also

operator /(AbsolutePath, LocalPath)

Works the same way as operator /(LocalPath, LocalPath) (read its documentation for the details, including the differences from Combine(string, string)), except that the result is always absolute: a path relative to the current directory of another drive gets resolved (see the 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. This is the algorithm of C++'s std::filesystem::path::operator/, except that the result is normalized, and that a path relative to the current directory of another drive gets resolved.

public static AbsolutePath operator /(AbsolutePath basePath, LocalPath b)

Parameters

basePath AbsolutePath
b LocalPath

Returns

AbsolutePath

Remarks

On Windows, a path relative to the current directory of another drive 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. E.g. C:\base / D:x is D:\x if the current directory of drive D: is its root. This is the only case when the result depends on the state of the process: operator /(LocalPath, LocalPath) returns D:x here.

On the same drive, such a path is resolved against the base path: C:\base / C:x is C:\base\x. A path rooted without a drive letter keeps the drive of the base path: C:\base / \x is C:\x.

See Also

operator ==(AbsolutePath, AbsolutePath)

Compares the path with another.

public static bool operator ==(AbsolutePath left, AbsolutePath right)

Parameters

left AbsolutePath
right AbsolutePath

Returns

bool

Remarks

Uses PlatformDefaultComparer for platform-default case sensitivity.

operator !=(AbsolutePath, AbsolutePath)

Compares the path with another.

public static bool operator !=(AbsolutePath left, AbsolutePath right)

Parameters

left AbsolutePath
right AbsolutePath

Returns

bool

Remarks

Uses PlatformDefaultComparer for platform-default case sensitivity.