FileSystemExtensions

Ecng.IO

Extension methods for IFileSystem.

メソッド

AppendAllText
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.
AppendAllTextAsync
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.

戻り値: A task representing the asynchronous operation.

BlockDeleteDirAsync
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.

戻り値: True if the directory still exists after deletion attempts; otherwise, false.

CheckDirContainFiles
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.

戻り値: True if the directory contains any files; otherwise false.

CheckDirContainsAnything
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.

戻り値: True if the directory contains any entries; otherwise false.

CheckInstallation
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.

戻り値: True if the directory exists and contains files or subdirectories; otherwise false.

ClearDirectory
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.

戻り値: A DirectoryInfo for the cleared directory.

ClearDirectoryAsync
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.

戻り値: A task that produces a DirectoryInfo for the cleared directory.

CopyAndMakeWritable
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.

戻り値: The destination file path.

CopyDirectory
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.
CopyDirectoryAsync
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.

戻り値: A task representing the asynchronous operation.

CreateDirIfNotExists
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.

戻り値: True if the directory was created; otherwise false if it already existed or path has no directory part.

CreateFile
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.
CreateTempDir
public static string CreateTempDir(IFileSystem fileSystem)
result = FileSystemExtensions.CreateTempDir(fileSystem)

Creates a temporary directory and returns its path.

fileSystem
The file system to use.

戻り値: The path to the new temporary directory.

DeleteEmptyDirs
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.
GetDirectories
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.

戻り値: An enumerable of matching directory paths. If the directory does not exist an empty enumerable is returned.

GetDirectoriesAsync
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.

戻り値: A task that produces an enumerable of matching directory paths.

GetFilesAsync
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.

戻り値: A task that produces an enumerable of matching file paths.

GetTimestamp
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.

戻り値: The timestamp representing the build time of the file (based on PE header), in local time.

IsDirectory
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.

戻り値: True if the path is a directory; otherwise false.

IsFileLocked
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.

戻り値: True if the file is locked; otherwise false.

OpenRead
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.

戻り値: A read-only stream.

OpenWrite
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.

戻り値: A write-capable stream.

ReadAllBytes
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.

戻り値: A byte array containing the file contents.

ReadAllBytesAsync
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.

戻り値: A task that produces a byte array containing the file contents.

ReadAllLines
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.

戻り値: An array of strings representing the file lines.

ReadAllLinesAsync
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.

戻り値: A task that produces an array of strings representing the file lines.

ReadAllText
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.

戻り値: File contents as string.

ReadAllTextAsync
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.

戻り値: File contents as string.

SafeDeleteDir
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.
Save
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.

戻り値: The original stream.

Save
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.

戻り値: The original byte array.

TrySave
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.

戻り値: True if the save operation was successful; otherwise, false.

WriteAllBytes
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.
WriteAllBytesAsync
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.

戻り値: A task representing the asynchronous operation.

WriteAllLines
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.
WriteAllLinesAsync
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.

戻り値: A task representing the asynchronous operation.

WriteAllText
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.
WriteAllTextAsync
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.

戻り値: A task representing the asynchronous operation.