Table of Contents

Class PathIo

Namespace
TruePath.SystemIo
Assembly
TruePath.SystemIo.dll

Extension methods for the AbsolutePath type that provide common I/O operations.

public static class PathIo
Inheritance
PathIo
Inherited Members

Methods

AppendAllText(AbsolutePath, string?)

Appends the specified string to the file, creating the file if it does not already exist.

public static void AppendAllText(this AbsolutePath path, string? contents)

Parameters

path AbsolutePath

The file to append the specified string to.

contents string

The string to append to the file.

AppendAllText(AbsolutePath, string?, Encoding)

Appends the specified string to the file using the specified encoding, creating the file if it does not already exist.

public static void AppendAllText(this AbsolutePath path, string? contents, Encoding encoding)

Parameters

path AbsolutePath

The file to append the specified string to.

contents string

The string to append to the file.

encoding Encoding

The character encoding to use.

CreateDirectory(AbsolutePath)

Creates all directories and subdirectories in the specified path unless they already exist.

public static void CreateDirectory(this AbsolutePath path)

Parameters

path AbsolutePath

The directory to create.

DeleteDirectoryRecursively(AbsolutePath)

Deletes the specified directory and any subdirectories and files in the directory.

public static void DeleteDirectoryRecursively(this AbsolutePath path)

Parameters

path AbsolutePath

The name of the directory to remove. This directory must be writable.

DeleteEmptyDirectory(AbsolutePath)

Deletes the specified empty directory.

public static void DeleteEmptyDirectory(this AbsolutePath path)

Parameters

path AbsolutePath

The name of the directory to remove. This directory must be writable and empty.

EnumerateDirectories(AbsolutePath)

Returns an enumerable collection of the absolute paths of the directories in a specified path.

public static IEnumerable<AbsolutePath> EnumerateDirectories(this AbsolutePath path)

Parameters

path AbsolutePath

The directory to search.

Returns

IEnumerable<AbsolutePath>

An enumerable collection of the absolute paths for the directories in the directory specified by path.

Exceptions

DirectoryNotFoundException

path is invalid, such as referring to an unmapped drive.

IOException

path is a file name.

PathTooLongException

The specified path, file name, or combined exceed the system-defined maximum length.

SecurityException

The caller does not have the required permission.

UnauthorizedAccessException

The caller does not have the required permission.

EnumerateDirectories(AbsolutePath, string)

Returns an enumerable collection of the absolute paths of the directories that match a search pattern in a specified path.

public static IEnumerable<AbsolutePath> EnumerateDirectories(this AbsolutePath path, string searchPattern)

Parameters

path AbsolutePath

The directory to search.

searchPattern string

The search string to match against the names of directories in path. It may combine literal characters with the * and ? wildcards, but it does not support regular expressions. This overload matches with Win32 semantics, where * stands for zero or more characters and ? for exactly one, except immediately before a period or at the end of the name, where it may also match none: a?.d matches both ab.d and a.d, while ?ello.d matches only hello.d.

When running on .NET Framework on Windows, the pattern is also matched against the short (8.3) name of each directory, if it has one. Since a short name keeps at most three characters of the extension, *.htm may also return site.html (whose short name is SITE~1.HTM), and a pattern such as *1* may match many directories with no 1 in their long names, through the ~1 suffix of their short names. Whether a directory has a short name depends on the volume settings at the time the directory was created, so the results may differ between machines and even between directories in the same parent directory. See Raymond Chen's explanation for details.

For the full pattern syntax and its caveats, see https://learn.microsoft.com/dotnet/api/system.io.directory.enumeratedirectories.

Returns

IEnumerable<AbsolutePath>

An enumerable collection of the absolute paths for the directories in the directory specified by path and that match the specified search pattern.

Exceptions

ArgumentNullException

searchPattern is null.

ArgumentException

searchPattern does not contain a valid pattern.

DirectoryNotFoundException

path is invalid, such as referring to an unmapped drive.

IOException

path is a file name.

PathTooLongException

The specified path, file name, or combined exceed the system-defined maximum length.

SecurityException

The caller does not have the required permission.

UnauthorizedAccessException

The caller does not have the required permission.

EnumerateDirectories(AbsolutePath, string, SearchOption)

Returns an enumerable collection of the absolute paths of the directories that match a search pattern in a specified path, and optionally searches subdirectories.

public static IEnumerable<AbsolutePath> EnumerateDirectories(this AbsolutePath path, string searchPattern, SearchOption searchOption)

Parameters

path AbsolutePath

The directory to search.

searchPattern string

The search string to match against the names of directories in path. It may combine literal characters with the * and ? wildcards, but it does not support regular expressions. This overload matches with Win32 semantics, where * stands for zero or more characters and ? for exactly one, except immediately before a period or at the end of the name, where it may also match none: a?.d matches both ab.d and a.d, while ?ello.d matches only hello.d.

When running on .NET Framework on Windows, the pattern is also matched against the short (8.3) name of each directory, if it has one. Since a short name keeps at most three characters of the extension, *.htm may also return site.html (whose short name is SITE~1.HTM), and a pattern such as *1* may match many directories with no 1 in their long names, through the ~1 suffix of their short names. Whether a directory has a short name depends on the volume settings at the time the directory was created, so the results may differ between machines and even between directories in the same parent directory. See Raymond Chen's explanation for details.

For the full pattern syntax and its caveats, see https://learn.microsoft.com/dotnet/api/system.io.directory.enumeratedirectories.

searchOption SearchOption

One of the enumeration values that specifies whether the search operation should include only the current directory or should include all subdirectories.

Returns

IEnumerable<AbsolutePath>

An enumerable collection of the absolute paths for the directories in the directory specified by path and that match the specified search pattern and search option.

Exceptions

ArgumentNullException

searchPattern is null.

ArgumentException

searchPattern does not contain a valid pattern.

ArgumentOutOfRangeException

searchOption is not a valid SearchOption value.

DirectoryNotFoundException

path is invalid, such as referring to an unmapped drive.

IOException

path is a file name.

PathTooLongException

The specified path, file name, or combined exceed the system-defined maximum length.

SecurityException

The caller does not have the required permission.

UnauthorizedAccessException

The caller does not have the required permission.

EnumerateFileSystemEntries(AbsolutePath)

Returns an enumerable collection of the absolute paths of the files and directories in a specified path.

public static IEnumerable<AbsolutePath> EnumerateFileSystemEntries(this AbsolutePath path)

Parameters

path AbsolutePath

The directory to search.

Returns

IEnumerable<AbsolutePath>

An enumerable collection of the absolute paths of the file-system entries in the directory specified by path.

Exceptions

DirectoryNotFoundException

path is invalid, such as referring to an unmapped drive.

IOException

path is a file name.

PathTooLongException

The specified path, file name, or combined exceed the system-defined maximum length.

SecurityException

The caller does not have the required permission.

UnauthorizedAccessException

The caller does not have the required permission.

EnumerateFileSystemEntries(AbsolutePath, string)

Returns an enumerable collection of the absolute paths of the files and directories that match a search pattern in a specified path.

public static IEnumerable<AbsolutePath> EnumerateFileSystemEntries(this AbsolutePath path, string searchPattern)

Parameters

path AbsolutePath

The directory to search.

searchPattern string

The search string to match against the names of file-system entries in path. It may combine literal characters with the * and ? wildcards, but it does not support regular expressions. This overload matches with Win32 semantics, where * stands for zero or more characters and ? for exactly one, except immediately before a period or at the end of the name, where it may also match none: a?.txt matches both ab.txt and a.txt, while ?ello.txt matches only hello.txt.

When running on .NET Framework on Windows, the pattern is also matched against the short (8.3) name of each file or directory, if it has one. Since a short name keeps at most three characters of the extension, *.htm may also return index.html (whose short name is INDEX~1.HTM), and a pattern such as *1* may match many entries with no 1 in their long names, through the ~1 suffix of their short names. Whether an entry has a short name depends on the volume settings at the time the entry was created, so the results may differ between machines and even between entries in the same directory. See Raymond Chen's explanation for details.

For the full pattern syntax and its caveats, see https://learn.microsoft.com/dotnet/api/system.io.directory.enumeratefilesystementries.

Returns

IEnumerable<AbsolutePath>

An enumerable collection of the absolute paths of the file-system entries in the directory specified by path and that match the specified search pattern.

Exceptions

ArgumentNullException

searchPattern is null.

ArgumentException

searchPattern does not contain a valid pattern.

DirectoryNotFoundException

path is invalid, such as referring to an unmapped drive.

IOException

path is a file name.

PathTooLongException

The specified path, file name, or combined exceed the system-defined maximum length.

SecurityException

The caller does not have the required permission.

UnauthorizedAccessException

The caller does not have the required permission.

EnumerateFileSystemEntries(AbsolutePath, string, SearchOption)

Returns an enumerable collection of the absolute paths of the files and directories that match a search pattern in a specified path, and optionally searches subdirectories.

public static IEnumerable<AbsolutePath> EnumerateFileSystemEntries(this AbsolutePath path, string searchPattern, SearchOption searchOption)

Parameters

path AbsolutePath

The directory to search.

searchPattern string

The search string to match against the names of file-system entries in path. It may combine literal characters with the * and ? wildcards, but it does not support regular expressions. This overload matches with Win32 semantics, where * stands for zero or more characters and ? for exactly one, except immediately before a period or at the end of the name, where it may also match none: a?.txt matches both ab.txt and a.txt, while ?ello.txt matches only hello.txt.

When running on .NET Framework on Windows, the pattern is also matched against the short (8.3) name of each file or directory, if it has one. Since a short name keeps at most three characters of the extension, *.htm may also return index.html (whose short name is INDEX~1.HTM), and a pattern such as *1* may match many entries with no 1 in their long names, through the ~1 suffix of their short names. Whether an entry has a short name depends on the volume settings at the time the entry was created, so the results may differ between machines and even between entries in the same directory. See Raymond Chen's explanation for details.

For the full pattern syntax and its caveats, see https://learn.microsoft.com/dotnet/api/system.io.directory.enumeratefilesystementries.

searchOption SearchOption

One of the enumeration values that specifies whether the search operation should include only the current directory or should include all subdirectories.

Returns

IEnumerable<AbsolutePath>

An enumerable collection of the absolute paths of the file-system entries in the directory specified by path and that match the specified search pattern and option.

Exceptions

ArgumentNullException

searchPattern is null.

ArgumentException

searchPattern does not contain a valid pattern.

ArgumentOutOfRangeException

searchOption is not a valid SearchOption value.

DirectoryNotFoundException

path is invalid, such as referring to an unmapped drive.

IOException

path is a file name.

PathTooLongException

The specified path, file name, or combined exceed the system-defined maximum length.

SecurityException

The caller does not have the required permission.

UnauthorizedAccessException

The caller does not have the required permission.

EnumerateFiles(AbsolutePath)

Returns an enumerable collection of AbsolutePath of full file names in a specified path.

public static IEnumerable<AbsolutePath> EnumerateFiles(this AbsolutePath path)

Parameters

path AbsolutePath

The directory to search.

Returns

IEnumerable<AbsolutePath>

An enumerable collection of AbsolutePath of the full names (including paths) for the files in the specified directory.

EnumerateFiles(AbsolutePath, string)

Returns an enumerable collection of AbsolutePath of full file names in a specified path.

public static IEnumerable<AbsolutePath> EnumerateFiles(this AbsolutePath path, string searchPattern)

Parameters

path AbsolutePath

The directory to search.

searchPattern string

The search string to match against the names of files in path. It may combine literal characters with the * and ? wildcards, but it does not support regular expressions. This overload matches with Win32 semantics, where * stands for zero or more characters and ? for exactly one, except immediately before a period or at the end of the name, where it may also match none: a?.txt matches both ab.txt and a.txt, while ?ello.txt matches only hello.txt.

When running on .NET Framework on Windows, the pattern is also matched against the short (8.3) name of each file, if it has one. Since a short name keeps at most three characters of the extension, *.htm may also return index.html (whose short name is INDEX~1.HTM), and a pattern such as *1* may match many files with no 1 in their long names, through the ~1 suffix of their short names. Whether a file has a short name depends on the volume settings at the time the file was created, so the results may differ between machines and even between files in the same directory. See Raymond Chen's explanation for details.

For the full pattern syntax and its caveats, see https://learn.microsoft.com/dotnet/api/system.io.directory.enumeratefiles.

Returns

IEnumerable<AbsolutePath>

An enumerable collection of AbsolutePath of the full names (including paths) for the files in the specified directory that match the specified search pattern.

EnumerateFiles(AbsolutePath, string, SearchOption)

Returns an enumerable collection of AbsolutePath with the names of files (including their paths) that match the specified search pattern in the specified directory, using a value to determine whether to search subdirectories.

public static IEnumerable<AbsolutePath> EnumerateFiles(this AbsolutePath path, string searchPattern, SearchOption searchOption)

Parameters

path AbsolutePath

The directory to search.

searchPattern string

The search string to match against the names of files in path. It may combine literal characters with the * and ? wildcards, but it does not support regular expressions. This overload matches with Win32 semantics, where * stands for zero or more characters and ? for exactly one, except immediately before a period or at the end of the name, where it may also match none: a?.txt matches both ab.txt and a.txt, while ?ello.txt matches only hello.txt.

When running on .NET Framework on Windows, the pattern is also matched against the short (8.3) name of each file, if it has one. Since a short name keeps at most three characters of the extension, *.htm may also return index.html (whose short name is INDEX~1.HTM), and a pattern such as *1* may match many files with no 1 in their long names, through the ~1 suffix of their short names. Whether a file has a short name depends on the volume settings at the time the file was created, so the results may differ between machines and even between files in the same directory. See Raymond Chen's explanation for details.

For the full pattern syntax and its caveats, see https://learn.microsoft.com/dotnet/api/system.io.directory.enumeratefiles.

searchOption SearchOption

One of the enumeration values that specifies whether the search operation should include only the current directory or should include all subdirectories.

Returns

IEnumerable<AbsolutePath>

An enumerable collection of AbsolutePath of the full names (including paths) for the files in the specified directory that match the specified search pattern and option.

Exists(AbsolutePath)

Determines whether the specified file or directory exists.

public static bool Exists(this AbsolutePath path)

Parameters

path AbsolutePath

The file or directory to check.

Returns

bool

true if the caller has the required permissions and path contains the name of an existing file or directory; otherwise, false. This method also returns false if path is null, an invalid path, or a zero-length string. If the caller does not have sufficient permissions to read the specified path, no exception is thrown and the method returns false regardless of the existence of path.

ExistsDirectory(AbsolutePath)

Determines whether the given path refers to an existing directory on disk.

public static bool ExistsDirectory(this AbsolutePath path)

Parameters

path AbsolutePath

The path to test.

Returns

bool

true if path refers to an existing directory; false if the directory does not exist or an error occurs when trying to determine if the specified directory exists.

ExistsFile(AbsolutePath)

Determines whether the specified file exists.

public static bool ExistsFile(this AbsolutePath path)

Parameters

path AbsolutePath

The file to check.

Returns

bool

true if the caller has the required permissions and path contains the name of an existing file; otherwise, false. This method also returns false if path is null, an invalid path, or a zero-length string. If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path.

GetFiles(AbsolutePath, LocalPathPattern)

Returns the names of files (including their paths) that match the specified search pattern in the specified directory.

public static AbsolutePath[] GetFiles(this AbsolutePath path, LocalPathPattern searchPattern)

Parameters

path AbsolutePath

The directory to search.

searchPattern LocalPathPattern

The search pattern to match against the names of files in path.

Returns

AbsolutePath[]

An array of the absolute paths for the files in the specified directory that match the specified search pattern.

WriteAllText(AbsolutePath, string?)

Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

public static void WriteAllText(this AbsolutePath path, string? contents)

Parameters

path AbsolutePath

The file to write to.

contents string

The string to write to the file.

WriteAllText(AbsolutePath, string?, Encoding)

Creates a new file, writes the specified string to the file using the specified encoding, and then closes the file. If the target file already exists, it is overwritten.

public static void WriteAllText(this AbsolutePath path, string? contents, Encoding encoding)

Parameters

path AbsolutePath

The file to write to.

contents string

The string to write to the file.

encoding Encoding

The encoding to apply to the string.