Provides extension methods for the DirectoryInfo class.
public static class DirectoryInfoExtensions
FileInfo CombineFileInfo(this DirectoryInfo directoryInfo, string[] paths)Summary: Combines the directory path with additional sub-paths to create a FileInfo object.
Parameters:
directoryInfo- The base directory information.
paths- An array of sub-paths to combine with the base directory.Returns: A FileInfo object representing the combined path.
long GetByteSize(this DirectoryInfo directoryInfo, string searchPattern = *.*, SearchOption searchOption = AllDirectories)Summary: Calculates the total size in bytes of all files in the directory matching the search pattern.
Parameters:
directoryInfo- The directory to calculate size for.
searchPattern- The search pattern to match file names against (e.g., ".txt"). Defaults to ".*" for all files.
searchOption- Specifies whether to search only the current directory or all subdirectories. Defaults to .Returns: The total size in bytes of all matching files.
FileInfo GetFileInfo(this DirectoryInfo directoryInfo, string file)Summary: Creates a
System.IO.FileInfoobject for a file within the directory.Parameters:
directoryInfo- The directory containing the file.
file- The file name (without path) to get information for.Returns: A
System.IO.FileInfoobject representing the combined directory and file name.
long GetFilesCount(this DirectoryInfo directoryInfo, string searchPattern = *.*, SearchOption searchOption = AllDirectories)Summary: Counts the total number of files in the directory matching the search pattern.
Parameters:
directoryInfo- The directory to search in.
searchPattern- The search pattern to match file names against (e.g., ".txt"). Defaults to ".*" for all files.
searchOption- Specifies whether to search only the current directory or all subdirectories. Defaults to .Returns: The total number of files found.
FileInfo[] GetFilesForAuthorizedAccess(this DirectoryInfo directoryInfo, string searchPattern = *.*, SearchOption searchOption = TopDirectoryOnly)Summary: Gets all files matching the search pattern while gracefully skipping files and folders with unauthorized access.
Parameters:
directoryInfo- The directory to search in.
searchPattern- The search pattern to match file names against (e.g., ".txt"). Defaults to ".*" for all files.
searchOption- Specifies whether to search only the current directory or all subdirectories. Defaults to .Returns: An array of
System.IO.FileInfoobjects representing the files found.
long GetFoldersCount(this DirectoryInfo directoryInfo, string searchPattern = *, SearchOption searchOption = AllDirectories)Summary: Counts the total number of subdirectories in the directory matching the search pattern.
Parameters:
directoryInfo- The directory to search in.
searchPattern- The search pattern to match directory names against (e.g., "temp*"). Defaults to "*" for all directories.
searchOption- Specifies whether to search only the current directory or all subdirectories. Defaults to .Returns: The total number of subdirectories found.
string GetPrettyByteSize(this DirectoryInfo directoryInfo, string searchPattern = *.*, SearchOption searchOption = AllDirectories)Summary: Gets the byte size as pretty formatted text like '1.933.212.103 bytes'.
Parameters:
directoryInfo- The directory information.
searchPattern- The search pattern.
searchOption- The search option.
string GetPrettySize(this DirectoryInfo directoryInfo, string searchPattern = *.*, SearchOption searchOption = AllDirectories)Summary: Gets the byte size as pretty formatted text like '1.82 GB'.
Parameters:
directoryInfo- The directory information.
searchPattern- The search pattern.
searchOption- The search option.
Extension methods for the System.IO.FileInfo class.
public static class FileInfoExtensions
byte[] ReadToByteArray(this FileInfo fileInfo)Summary: Reads all bytes from the file into a byte array.
Parameters:
fileInfo- The file information representing the file to read.Returns: A byte array containing all data from the file.
Task<byte[]> ReadToByteArrayAsync(this FileInfo fileInfo, CancellationToken cancellationToken = null)Summary: Asynchronously reads all bytes from the file into a byte array.
Parameters:
fileInfo- The file information representing the file to read.
cancellationToken- A cancellation token to observe while waiting for the asynchronous operation to complete.Returns: A task that represents the asynchronous read operation. The task result contains a byte array with all data from the file.
MemoryStream ReadToMemoryStream(this FileInfo fileInfo)Summary: Reads the file content into a
System.IO.MemoryStream.Parameters:
fileInfo- The file information representing the file to read.Returns: A
System.IO.MemoryStreamcontaining the file content with position set to 0.
Task<MemoryStream> ReadToMemoryStreamAsync(this FileInfo fileInfo, CancellationToken cancellationToken = null)Summary: Asynchronously reads the file content into a
System.IO.MemoryStream.Parameters:
fileInfo- The file information representing the file to read.
cancellationToken- A cancellation token to observe while waiting for the asynchronous operation to complete.Returns: A task that represents the asynchronous read operation. The task result contains a
System.IO.MemoryStreamwith the file content and position set to 0.
Extensions for the System.IO.MemoryStream class.
public static class MemoryStreamExtensions
string ToString(this MemoryStream stream, Encoding encoding = null)Summary: Converts the memory stream content to a string using the specified encoding.
Parameters:
stream- The memory stream to convert.
encoding- The encoding to use for the conversion. Defaults to when .Returns: A string representation of the memory stream content.
Extensions for the System.IO.Stream class.
public static class StreamExtensions
Stream CopyToStream(this Stream stream, int bufferSize = 4096)Summary: Copies the contents of the stream to a new
System.IO.MemoryStream.Parameters:
stream- The source stream to copy from. The stream position will be reset to 0 before copying.
bufferSize- The size of the buffer used for copying. Defaults to 4096 bytes.Returns: A new
System.IO.MemoryStreamcontaining the copied data with position set to 0.
Task<Stream> CopyToStreamAsync(this Stream stream, int bufferSize = 4096, CancellationToken cancellationToken = null)Summary: Asynchronously copies the contents of the stream to a new
System.IO.MemoryStream.Parameters:
stream- The source stream to copy from. The stream position will be reset to 0 before copying if the stream supports seeking.
bufferSize- The size of the buffer used for copying. Defaults to 4096 bytes.
cancellationToken- A token to cancel the asynchronous operation.Returns: A
System.Threading.Tasks.Task1that represents the asynchronous operation, containing a newSystem.IO.MemoryStream` with position set to 0.
byte[] ToBytes(this Stream stream)Summary: Reads all bytes from the stream and returns them as a byte array.
Parameters:
stream- The stream to read from. The stream position will be reset to 0 before reading.Returns: A byte array containing all data from the stream.
Task<byte[]> ToBytesAsync(this Stream stream, CancellationToken cancellationToken = null)Summary: Asynchronously reads all bytes from the stream and returns them as a byte array.
Parameters:
stream- The stream to read from. The stream position will be reset to 0 before reading if the stream supports seeking.
cancellationToken- A token to cancel the asynchronous operation.Returns: A
System.Threading.Tasks.Task1` that represents the asynchronous operation, containing a byte array with all data from the stream.
string ToStringData(this Stream stream)Summary: Reads all content from the stream and converts it to a string using the default encoding.
Parameters:
stream- The stream to read from. The stream position will be reset to 0 before reading.Returns: A string containing all text content from the stream.
Task<string> ToStringDataAsync(this Stream stream, CancellationToken cancellationToken = null)Summary: Asynchronously reads all content from the stream and converts it to a string using UTF-8 encoding.
Parameters:
stream- The stream to read from. The stream position will be reset to 0 before reading if the stream supports seeking.
cancellationToken- A token to cancel the asynchronous operation.Returns: A
System.Threading.Tasks.Task1` that represents the asynchronous operation, containing a string with all text content from the stream.