Class PathIo
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
pathAbsolutePathThe file to append the specified string to.
contentsstringThe 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
pathAbsolutePathThe file to append the specified string to.
contentsstringThe string to append to the file.
encodingEncodingThe 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
pathAbsolutePathThe 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
pathAbsolutePathThe 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
pathAbsolutePathThe 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
pathAbsolutePathThe directory to search.
Returns
- IEnumerable<AbsolutePath>
An enumerable collection of the absolute paths for the directories in the directory specified by
path.
Exceptions
- DirectoryNotFoundException
pathis invalid, such as referring to an unmapped drive.- IOException
pathis 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
pathAbsolutePathThe directory to search.
searchPatternstringThe 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?.dmatches bothab.danda.d, while?ello.dmatches onlyhello.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,
*.htmmay also returnsite.html(whose short name isSITE~1.HTM), and a pattern such as*1*may match many directories with no1in their long names, through the~1suffix 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
pathand that match the specified search pattern.
Exceptions
- ArgumentNullException
searchPatternis null.- ArgumentException
searchPatterndoes not contain a valid pattern.- DirectoryNotFoundException
pathis invalid, such as referring to an unmapped drive.- IOException
pathis 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
pathAbsolutePathThe directory to search.
searchPatternstringThe 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?.dmatches bothab.danda.d, while?ello.dmatches onlyhello.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,
*.htmmay also returnsite.html(whose short name isSITE~1.HTM), and a pattern such as*1*may match many directories with no1in their long names, through the~1suffix 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.
searchOptionSearchOptionOne 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
pathand that match the specified search pattern and search option.
Exceptions
- ArgumentNullException
searchPatternis null.- ArgumentException
searchPatterndoes not contain a valid pattern.- ArgumentOutOfRangeException
searchOptionis not a valid SearchOption value.- DirectoryNotFoundException
pathis invalid, such as referring to an unmapped drive.- IOException
pathis 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
pathAbsolutePathThe 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
pathis invalid, such as referring to an unmapped drive.- IOException
pathis 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
pathAbsolutePathThe directory to search.
searchPatternstringThe 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?.txtmatches bothab.txtanda.txt, while?ello.txtmatches onlyhello.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,
*.htmmay also returnindex.html(whose short name isINDEX~1.HTM), and a pattern such as*1*may match many entries with no1in their long names, through the~1suffix 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
pathand that match the specified search pattern.
Exceptions
- ArgumentNullException
searchPatternis null.- ArgumentException
searchPatterndoes not contain a valid pattern.- DirectoryNotFoundException
pathis invalid, such as referring to an unmapped drive.- IOException
pathis 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
pathAbsolutePathThe directory to search.
searchPatternstringThe 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?.txtmatches bothab.txtanda.txt, while?ello.txtmatches onlyhello.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,
*.htmmay also returnindex.html(whose short name isINDEX~1.HTM), and a pattern such as*1*may match many entries with no1in their long names, through the~1suffix 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.
searchOptionSearchOptionOne 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
pathand that match the specified search pattern and option.
Exceptions
- ArgumentNullException
searchPatternis null.- ArgumentException
searchPatterndoes not contain a valid pattern.- ArgumentOutOfRangeException
searchOptionis not a valid SearchOption value.- DirectoryNotFoundException
pathis invalid, such as referring to an unmapped drive.- IOException
pathis 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
pathAbsolutePathThe 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
pathAbsolutePathThe directory to search.
searchPatternstringThe 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?.txtmatches bothab.txtanda.txt, while?ello.txtmatches onlyhello.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,
*.htmmay also returnindex.html(whose short name isINDEX~1.HTM), and a pattern such as*1*may match many files with no1in their long names, through the~1suffix 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
pathAbsolutePathThe directory to search.
searchPatternstringThe 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?.txtmatches bothab.txtanda.txt, while?ello.txtmatches onlyhello.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,
*.htmmay also returnindex.html(whose short name isINDEX~1.HTM), and a pattern such as*1*may match many files with no1in their long names, through the~1suffix 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.
searchOptionSearchOptionOne 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
pathAbsolutePathThe file or directory to check.
Returns
- bool
true if the caller has the required permissions and
pathcontains the name of an existing file or directory; otherwise, false. This method also returns false ifpathis 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 ofpath.
ExistsDirectory(AbsolutePath)
Determines whether the given path refers to an existing directory on disk.
public static bool ExistsDirectory(this AbsolutePath path)
Parameters
pathAbsolutePathThe 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
pathAbsolutePathThe file to check.
Returns
- bool
true if the caller has the required permissions and
pathcontains the name of an existing file; otherwise, false. This method also returns false ifpathis 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 ofpath.
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
pathAbsolutePathThe directory to search.
searchPatternLocalPathPatternThe 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
pathAbsolutePathThe file to write to.
contentsstringThe 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
pathAbsolutePathThe file to write to.
contentsstringThe string to write to the file.
encodingEncodingThe encoding to apply to the string.