FileSystemExtensions
Extension methods for IFileSystem.
Métodos
public static void AppendAllText(IFileSystem fs, string path, string content, Encoding encoding)
FileSystemExtensions.AppendAllText(fs, path, content, encoding)
Appends text to a file.
- fs
- The file system to use.
- path
- The path to the file to append to.
- content
- The text content to append.
- encoding
- The text encoding to use. If null, UTF8 is used.
public static Task AppendAllTextAsync(IFileSystem fs, string path, string content, Encoding encoding, CancellationToken cancellationToken)
result = FileSystemExtensions.AppendAllTextAsync(fs, path, content, encoding, cancellationToken)
Asynchronously appends text to a file.
- fs
- The file system to use.
- path
- The path to the file to append to.
- content
- The text content to append.
- encoding
- The text encoding to use. If null, UTF8 is used.
- cancellationToken
- A cancellation token to cancel the operation.
Devuelve: A task representing the asynchronous operation.
public static ValueTask<bool> BlockDeleteDirAsync(IFileSystem fileSystem, string dir, bool isRecursive, int iterCount, int sleep, CancellationToken cancellationToken)
result = FileSystemExtensions.BlockDeleteDirAsync(fileSystem, dir, isRecursive, iterCount, sleep, cancellationToken)
Deletes a directory in a blocking manner.
- fileSystem
- The file system to use.
- dir
- The directory to delete.
- isRecursive
- Indicates whether to delete subdirectories recursively.
- iterCount
- Number of iterations to attempt deletion.
- sleep
- Sleep duration between attempts in milliseconds.
- cancellationToken
- A token to monitor for cancellation requests.
Devuelve: True if the directory still exists after deletion attempts; otherwise, false.
public static bool CheckDirContainFiles(IFileSystem fs, string path)
result = FileSystemExtensions.CheckDirContainFiles(fs, path)
Checks whether the directory contains files or subdirectories that contain files.
- fs
- The file system to use.
- path
- The directory path to check.
Devuelve: True if the directory contains any files; otherwise false.
public static bool CheckDirContainsAnything(IFileSystem fs, string path)
result = FileSystemExtensions.CheckDirContainsAnything(fs, path)
Checks whether the directory contains any files or subdirectories.
- fs
- The file system to use.
- path
- The directory path to check.
Devuelve: True if the directory contains any entries; otherwise false.
public static bool CheckInstallation(IFileSystem fs, string path)
result = FileSystemExtensions.CheckInstallation(fs, path)
Checks whether the specified installation directory exists and contains files or subdirectories.
- fs
- The file system to use.
- path
- The installation directory path to check.
Devuelve: True if the directory exists and contains files or subdirectories; otherwise false.
public static DirectoryInfo ClearDirectory(IFileSystem fs, string path, Func<string, bool> filter)
result = FileSystemExtensions.ClearDirectory(fs, path, filter)
Clears the specified directory by deleting its files and subdirectories using the provided file system.
- fs
- The file system to use.
- path
- The directory path to clear.
- filter
- Optional filter to determine which files to delete. If null, all files are deleted.
Devuelve: A DirectoryInfo for the cleared directory.
public static Task<DirectoryInfo> ClearDirectoryAsync(IFileSystem fs, string path, Func<string, bool> filter, CancellationToken cancellationToken)
result = FileSystemExtensions.ClearDirectoryAsync(fs, path, filter, cancellationToken)
Asynchronously clears the specified directory by deleting its files and subdirectories using the provided file system.
- fs
- The file system to use.
- path
- The directory path to clear.
- filter
- Optional filter to determine which files to delete. If null, all files are deleted.
- cancellationToken
- A cancellation token to cancel the operation.
Devuelve: A task that produces a DirectoryInfo for the cleared directory.
public static string CopyAndMakeWritable(IFileSystem fs, string fileName, string destPath)
result = FileSystemExtensions.CopyAndMakeWritable(fs, fileName, destPath)
Copies a file to the specified destination and makes the copy writable using the provided file system.
- fs
- The file system to use.
- fileName
- The source file path.
- destPath
- The destination directory path.
Devuelve: The destination file path.
public static void CopyDirectory(IFileSystem fs, string sourcePath, string destPath)
FileSystemExtensions.CopyDirectory(fs, sourcePath, destPath)
Copies the content of one directory to another using the provided file system.
- fs
- The file system to use.
- sourcePath
- The source directory path.
- destPath
- The destination directory path.
public static Task CopyDirectoryAsync(IFileSystem fs, string sourcePath, string destPath, CancellationToken cancellationToken)
result = FileSystemExtensions.CopyDirectoryAsync(fs, sourcePath, destPath, cancellationToken)
Asynchronously copies the content of one directory to another using the provided file system.
- fs
- The file system to use.
- sourcePath
- The source directory path.
- destPath
- The destination directory path.
- cancellationToken
- A cancellation token to cancel the operation.
Devuelve: A task representing the asynchronous operation.
public static bool CreateDirIfNotExists(IFileSystem fs, string fullPath)
result = FileSystemExtensions.CreateDirIfNotExists(fs, fullPath)
Creates the directory for the specified file if it does not already exist.
- fs
- The file system to use.
- fullPath
- The full file path for which to ensure the directory exists.
Devuelve: True if the directory was created; otherwise false if it already existed or path has no directory part.
public static void CreateFile(IFileSystem fs, string rootPath, string relativePath, string fileName, byte[] content)
FileSystemExtensions.CreateFile(fs, rootPath, relativePath, fileName, content)
Creates a file with the specified content.
- fs
- The file system to use.
- rootPath
- The root path.
- relativePath
- The relative path to the file.
- fileName
- The file name.
- content
- The content as a byte array.
public static string CreateTempDir(IFileSystem fileSystem)
result = FileSystemExtensions.CreateTempDir(fileSystem)
Creates a temporary directory and returns its path.
- fileSystem
- The file system to use.
Devuelve: The path to the new temporary directory.
public static void DeleteEmptyDirs(IFileSystem fs, string dir)
FileSystemExtensions.DeleteEmptyDirs(fs, dir)
Recursively deletes empty directories starting from the specified directory.
- fs
- The file system to use.
- dir
- The root directory to check and delete if empty.
public static IEnumerable<string> GetDirectories(IFileSystem fs, string path, string searchPattern, SearchOption searchOption)
result = FileSystemExtensions.GetDirectories(fs, path, searchPattern, searchOption)
Retrieves the directories within the specified path matching the search pattern.
- fs
- The file system to use.
- path
- The root directory to search.
- searchPattern
- The search pattern (supports '*' and '?').
- searchOption
- Search option specifying whether to search subdirectories.
Devuelve: An enumerable of matching directory paths. If the directory does not exist an empty enumerable is returned.
public static Task<IEnumerable<string>> GetDirectoriesAsync(IFileSystem fs, string path, string searchPattern, SearchOption searchOption, CancellationToken cancellationToken)
result = FileSystemExtensions.GetDirectoriesAsync(fs, path, searchPattern, searchOption, cancellationToken)
Asynchronously retrieves the directories within the specified path matching the search pattern.
- fs
- The file system to use.
- path
- The root directory to search.
- searchPattern
- The search pattern.
- searchOption
- Search option specifying whether to search subdirectories.
- cancellationToken
- A cancellation token.
Devuelve: A task that produces an enumerable of matching directory paths.
public static Task<IEnumerable<string>> GetFilesAsync(IFileSystem fs, string path, string searchPattern, SearchOption searchOption, CancellationToken cancellationToken)
result = FileSystemExtensions.GetFilesAsync(fs, path, searchPattern, searchOption, cancellationToken)
Asynchronously retrieves the files within the specified path matching the search pattern.
- fs
- The file system to use.
- path
- The root directory to search.
- searchPattern
- The search pattern.
- searchOption
- Search option specifying whether to search subdirectories.
- cancellationToken
- A cancellation token.
Devuelve: A task that produces an enumerable of matching file paths.
public static DateTime GetTimestamp(IFileSystem fs, string filePath)
result = FileSystemExtensions.GetTimestamp(fs, filePath)
Gets the timestamp of the specified file using provided file system.
- fs
- The file system to use.
- filePath
- The file path to inspect.
Devuelve: The timestamp representing the build time of the file (based on PE header), in local time.
public static bool IsDirectory(IFileSystem fileSystem, string path)
result = FileSystemExtensions.IsDirectory(fileSystem, path)
Determines whether the specified path represents a directory.
- fileSystem
- The file system to use.
- path
- The path to check.
Devuelve: True if the path is a directory; otherwise false.
public static bool IsFileLocked(IFileSystem fs, string path)
result = FileSystemExtensions.IsFileLocked(fs, path)
Determines whether the file specified by the path is locked by another process.
- fs
- The file system to use.
- path
- The file path to check.
Devuelve: True if the file is locked; otherwise false.
public static Stream OpenRead(IFileSystem fs, string path)
result = FileSystemExtensions.OpenRead(fs, path)
Opens a readable stream for the file at the specified path.
- fs
- File system.
- path
- Path to the file.
Devuelve: A read-only stream.
public static Stream OpenWrite(IFileSystem fs, string path, bool append)
result = FileSystemExtensions.OpenWrite(fs, path, append)
Opens a writable stream for the file at the specified path.
- fs
- File system.
- path
- Path to the file.
- append
- If true, appends to the file; otherwise overwrites.
Devuelve: A write-capable stream.
public static byte[] ReadAllBytes(IFileSystem fs, string path)
result = FileSystemExtensions.ReadAllBytes(fs, path)
Reads all bytes from a file.
- fs
- The file system to use.
- path
- The path to the file to read.
Devuelve: A byte array containing the file contents.
public static Task<byte[]> ReadAllBytesAsync(IFileSystem fs, string path, CancellationToken cancellationToken)
result = FileSystemExtensions.ReadAllBytesAsync(fs, path, cancellationToken)
Asynchronously reads all bytes from a file.
- fs
- The file system to use.
- path
- The path to the file to read.
- cancellationToken
- A cancellation token to cancel the operation.
Devuelve: A task that produces a byte array containing the file contents.
public static string[] ReadAllLines(IFileSystem fs, string path, Encoding encoding)
result = FileSystemExtensions.ReadAllLines(fs, path, encoding)
Reads all lines from a file.
- fs
- The file system to use.
- path
- The path to the file to read.
- encoding
- The text encoding to use. If null, UTF8 is used.
Devuelve: An array of strings representing the file lines.
public static Task<string[]> ReadAllLinesAsync(IFileSystem fs, string path, Encoding encoding, CancellationToken cancellationToken)
result = FileSystemExtensions.ReadAllLinesAsync(fs, path, encoding, cancellationToken)
Asynchronously reads all lines from a file.
- fs
- The file system to use.
- path
- The path to the file to read.
- encoding
- The text encoding to use. If null, UTF8 is used.
- cancellationToken
- A cancellation token to cancel the operation.
Devuelve: A task that produces an array of strings representing the file lines.
public static string ReadAllText(IFileSystem fs, string path, Encoding encoding)
result = FileSystemExtensions.ReadAllText(fs, path, encoding)
Reads all text from a file.
- fs
- File system.
- path
- Path to the file.
- encoding
- Text encoding. Defaults to UTF8.
Devuelve: File contents as string.
public static Task<string> ReadAllTextAsync(IFileSystem fs, string path, Encoding encoding, CancellationToken cancellationToken)
result = FileSystemExtensions.ReadAllTextAsync(fs, path, encoding, cancellationToken)
Reads all text from a file asynchronously.
- fs
- File system.
- path
- Path to the file.
- encoding
- Text encoding. Defaults to UTF8.
- cancellationToken
- Cancellation token.
Devuelve: File contents as string.
public static void SafeDeleteDir(IFileSystem fs, string path)
FileSystemExtensions.SafeDeleteDir(fs, path)
Safely deletes a directory if it exists.
- fs
- The file system to use.
- path
- The directory path to delete.
public static Stream Save(IFileSystem fs, Stream stream, string fileName)
result = FileSystemExtensions.Save(fs, stream, fileName)
Saves the content of the stream to a file specified by fileName.
- fs
- The file system to use.
- stream
- The stream whose contents to save.
- fileName
- The file path to save the stream's contents to.
Devuelve: The original stream.
public static byte[] Save(IFileSystem fs, byte[] data, string fileName)
result = FileSystemExtensions.Save(fs, data, fileName)
Saves the byte array to a file specified by fileName.
- fs
- The file system to use.
- data
- The byte array to save.
- fileName
- The file path to save the data to.
Devuelve: The original byte array.
public static bool TrySave(IFileSystem fs, byte[] data, string fileName, Action<Exception> errorHandler)
result = FileSystemExtensions.TrySave(fs, data, fileName, errorHandler)
Attempts to save the byte array to a file and handles any exceptions using the provided errorHandler.
- fs
- The file system to use.
- data
- The byte array to save.
- fileName
- The file path to save the data to.
- errorHandler
- The action to handle exceptions.
Devuelve: True if the save operation was successful; otherwise, false.
public static void WriteAllBytes(IFileSystem fs, string path, byte[] bytes)
FileSystemExtensions.WriteAllBytes(fs, path, bytes)
Writes bytes to a file.
- fs
- The file system to use.
- path
- The path to the file to write.
- bytes
- The byte array to write.
public static Task WriteAllBytesAsync(IFileSystem fs, string path, byte[] bytes, CancellationToken cancellationToken)
result = FileSystemExtensions.WriteAllBytesAsync(fs, path, bytes, cancellationToken)
Asynchronously writes bytes to a file.
- fs
- The file system to use.
- path
- The path to the file to write.
- bytes
- The byte array to write.
- cancellationToken
- A cancellation token to cancel the operation.
Devuelve: A task representing the asynchronous operation.
public static void WriteAllLines(IFileSystem fs, string path, IEnumerable<string> lines, Encoding encoding)
FileSystemExtensions.WriteAllLines(fs, path, lines, encoding)
Writes lines to a file.
- fs
- The file system to use.
- path
- The path to the file to write.
- lines
- The lines to write.
- encoding
- The text encoding to use. If null, UTF8 is used.
public static Task WriteAllLinesAsync(IFileSystem fs, string path, IEnumerable<string> lines, Encoding encoding, CancellationToken cancellationToken)
result = FileSystemExtensions.WriteAllLinesAsync(fs, path, lines, encoding, cancellationToken)
Asynchronously writes lines to a file.
- fs
- The file system to use.
- path
- The path to the file to write.
- lines
- The lines to write.
- encoding
- The text encoding to use. If null, UTF8 is used.
- cancellationToken
- A cancellation token to cancel the operation.
Devuelve: A task representing the asynchronous operation.
public static void WriteAllText(IFileSystem fs, string path, string content, Encoding encoding)
FileSystemExtensions.WriteAllText(fs, path, content, encoding)
Writes text to a file.
- fs
- File system.
- path
- Path to the file.
- content
- Text content.
- encoding
- Text encoding. Defaults to UTF8.
public static Task WriteAllTextAsync(IFileSystem fs, string path, string content, Encoding encoding, CancellationToken cancellationToken)
result = FileSystemExtensions.WriteAllTextAsync(fs, path, content, encoding, cancellationToken)
Asynchronously writes text to a file.
- fs
- The file system to use.
- path
- The path to the file to write.
- content
- The text content to write.
- encoding
- The text encoding to use. If null, UTF8 is used.
- cancellationToken
- A cancellation token to cancel the operation.
Devuelve: A task representing the asynchronous operation.