Spargine is a collection of open-source assemblies and NuGet packages designed for .NET 10, which I have been developing and maintaining since the release of .NET Framework 2. These assemblies are not only a core part of my projects but are also actively deployed in production environments across several companies I collaborate with.
Get Spargine
You can access the source code and NuGet packages here:
- GitHub: Spargine 10
- NuGet: dotNetDaveNuGet
If you work with file I/O on Windows, Spargine’s FileHelper, part of the DotNetTips.Spargine project and NuGet package—is a must-have. Designed to streamline file operations, improve performance, and minimize errors, FileHelper provides an intuitive API to handle common file-related tasks efficiently.
With years of experience working on file I/O, I find FileHelper indispensable for handling file permissions, copying, moving, deleting, and even downloading files from the web. Below, I will explore its methods and benefits and how to use them effectively.
FileHelper Methods
FileHelper is optimized for performance and error prevention. Here is a breakdown of its key methods:
File Management
- AddAttributes(FileInfo file, FileAttributes attributesToAdd)
Adds the specified FileAttributes to a file. - AddReadOnlyAttribute(FileInfo file)
Adds the read-only attribute to a file. - CheckPermission(FileInfo file, FileSystemRights permission)
Checks if the specified file has the given permission for the current user. - CopyFile(FileInfo file, DirectoryInfo destination)
Copies a file to a new location. - CopyFile(FileInfo file, DirectoryInfo destination, CopyProgressRoutine progressCallback)
Copies a specified file to a new location with progress reporting. - CopyFileAsync(FileInfo file, DirectoryInfo destination, CancellationToken cancellationToken)
Asynchronously copies a specified file to a new location. - MoveFile(FileInfo file, FileInfo destinationFile, FileMoveOptions fileMoveOptions, int retryCount)
Moves a file to a new location, optionally replacing the destination file with retry logic. - RemoveArchiveAttribute(FileInfo file)
Removes the archive attribute from a file. - RemoveAttributes(FileInfo file, FileAttributes attributesToRemove)
Removes the specified FileAttributes from a file. - RemoveReadOnlyAttribute(FileInfo file)
Removes the read-only attribute from a file.
File Deletion & Validation
- DeleteFiles(ReadOnlyCollection<FileInfo> files, bool stopOnFirstError)
Deletes multiple files, stopping on the first error if specified. - FileHasInvalidChars(FileInfo file)
Checks if the specified file contains any invalid characters in its name.
Web File Operations
- DownloadFileFromWebAndUnzipAsync(Uri remoteUri, DirectoryInfo destination, CancellationToken cancellationToken)
Downloads a file from the web and unzips it to the specified destination directory. - DownloadFileFromWebAsync(Uri remoteUri, DirectoryInfo destination, CancellationToken cancellationToken)
Asynchronously downloads a file from the specified Uri and saves it to the given destination directory.
Compression & Extraction
- UnGZipAsync(FileInfo file, DirectoryInfo destination, bool deleteGZipFile, CancellationToken cancellationToken)
Asynchronously decompresses a GZip (.gz) file to the specified destination directory. - UnWinZipAsync(string zipPath, string expandedDirectoryPath, CancellationToken cancellationToken)
Asynchronously extracts the contents of a Windows compressed (zipped) folder to the specified directory. - UnZipAsync(FileInfo file, DirectoryInfo destination, bool deleteZipFile, CancellationToken cancellationToken)
Asynchronously extracts a ZIP file, with an option to delete the original. - UnZipAsync(FileInfo file, DirectoryInfo destination, CancellationToken cancellationToken)
Asynchronously unzips a specified zip file to the given destination directory.
FileHelper Properties
- InvalidFileNameChars
Gets a read-only collection of characters that are not allowed in file names, excluding directory separator characters.
Code Examples
rEMOVE fILE aTTRIBUTES
var files = folder.EnumerateFiles(ControlChars.WildcardAllFiles, SearchOption.AllDirectories).Where(p => p.IsReadOnly);
_ = Parallel.ForEach(files, FileHelper.RemoveReadOnlyAttribute);
Why Use FileHelper?
FileHelper simplifies your file I/O operations, reduces boilerplate code, and improves error handling. Whether you are working with local files, web downloads, or compressed archives, this tool provides a high-performance, developer-friendly API.
If your project requires directory management, consider using DirectoryHelper, which complements FileHelper with additional utilities.
Get Involved!
The success of open-source projects like Spargine relies on community contributions. If you find these updates useful or have ideas for further improvements, I encourage you to contribute by:
- Submitting pull requests
- Reporting issues
- Suggesting new features
Your input is invaluable in making Spargine an even more powerful tool for the .NET community.
If you are interested in contributing or have any questions, feel free to contact me via email at dotnetdave@live.com. Your support and collaboration are greatly appreciated!
Thank you, and happy coding!
Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
DonateDonate monthlyDonate yearlyIf you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave
© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.

