Path

Source: lib/Path.cs

This page is generated from the source by scripts/gen-reference.py.

Paths: the parts of a path, joining paths, and tidying one up. All of it works on the text alone, on every system, without touching the disk (except GetFullPath, which asks for the current directory).

Path.GetFileName("logs/today.txt")          "today.txt"
Path.GetExtension("logs/today.txt")         ".txt"
Path.GetDirectoryName("logs/today.txt")     "logs"
Path.Combine("logs", "today.txt")           "logs/today.txt" ("logs\today.txt" on Windows)
Path.Normalize("logs/./old/../today.txt")   "logs/today.txt"

'/' separates names everywhere; on Windows, DOS and UEFI so does '', which is also what Combine and Normalize write there. The functions that pick out a part of a path return a slice of it and allocate nothing; the ones that make a new path return a string, and have a Try form that writes into a span instead.

Functions on paths, as text: their parts, joining them, and tidying them up.

Members

Separator

public static char Separator

The separator Combine and Normalize write: '' on Windows, DOS and UEFI, '/' elsewhere.

IsSeparator

public static bool IsSeparator(char c)

Whether c separates names: '/', and on Windows, DOS and UEFI also ''.

IsRooted

public static bool IsRooted(ReadOnlySpan<char> path)

Whether the path starts from a root rather than the current directory: "/x", and on Windows, DOS and UEFI also "\x", "C:x", "C:\x" and "\server\share".

GetRoot

public static ReadOnlySpan<char> GetRoot(ReadOnlySpan<char> path)

The root the path starts with ("/", "C:", "\server\share"), or an empty span for a relative path.

GetFileName

public static ReadOnlySpan<char> GetFileName(ReadOnlySpan<char> path)

The last name in the path: "c.txt" in "a/b/c.txt". Empty if the path ends with a separator.

GetFileNameWithoutExtension

public static ReadOnlySpan<char> GetFileNameWithoutExtension(
    ReadOnlySpan<char> path)

The file name without its extension: "c" in "a/b/c.txt", "archive.tar" in "archive.tar.gz", and "" in ".profile".

GetExtension

public static ReadOnlySpan<char> GetExtension(ReadOnlySpan<char> path)

The extension, with its dot: ".txt" in "a/b/c.txt", ".gz" in "archive.tar.gz". Empty when the file name has no dot, or ends with one.

HasExtension

public static bool HasExtension(ReadOnlySpan<char> path)

Whether the file name has an extension, as GetExtension finds it.

GetDirectoryName

public static ReadOnlySpan<char> GetDirectoryName(ReadOnlySpan<char> path)

The path without its last name: "a/b" for "a/b/c.txt", "/" for "/a". Empty for a name on its own ("c.txt") and for a root ("/", "C:").

Combine

public static string Combine(
    ReadOnlySpan<char> first,
    ReadOnlySpan<char> second)

second after first, with a separator between them if first doesn't end with one. If second is rooted it's the answer on its own, and an empty path adds nothing.

public static string Combine(
    ReadOnlySpan<char> first,
    ReadOnlySpan<char> second,
    ReadOnlySpan<char> third)

The three paths combined, as Combine(Combine(first, second), third).

TryCombine

public static bool TryCombine(
    ReadOnlySpan<char> first,
    ReadOnlySpan<char> second,
    Span<char> destination,
    out int written)

Combine, into destination. False, with nothing written, if it doesn't fit.

ChangeExtension

public static string ChangeExtension(
    ReadOnlySpan<char> path,
    ReadOnlySpan<char> extension)

The path with its extension replaced: ChangeExtension("a/b.txt", ".md") is "a/b.md". The dot is added if extension has none, and an empty extension removes the extension.

Normalize

public static string Normalize(ReadOnlySpan<char> path)

The path tidied up, from the text alone: separators become Separator, repeated ones become one, "." names go, and each ".." takes away the name before it. A relative path keeps the ".." it starts with; a rooted one can't go above its root. A trailing separator goes, and a path that ends up empty is ".". Symbolic links aren't looked at, so "link/.." may not be where the system would go.

TryNormalize

public static bool TryNormalize(
    ReadOnlySpan<char> path,
    Span<char> destination,
    out int written)

Normalize, into destination. False, with nothing written, if it doesn't fit (path.Length + 1 chars always do).

GetFullPath

public static string GetFullPath(ReadOnlySpan<char> path)

The path made absolute: combined with the current directory unless it's rooted, then normalized. Linux, macOS, Windows and WebAssembly; on Windows a path rooted only by its drive ("C:x") or a '' ("\x") is normalized as it is.