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
Working with files often takes more than a single API call. You may need to create a directory before writing, avoid leaving a partially written file, return a fallback when a read fails, or total the sizes of selected files. Spargine’s DirectoryInfoExtensions brings these common operations to DirectoryInfo through four extension methods.
Methods
- CreateTempFileThenMove(string destinationFileName, string content, Encoding? encoding = null)
Creates the directory if needed, then delegates the write to FileInfoExtensions.CreateTempFileThenMove(). The content is written through a temporary file before being moved to the destination, reducing the risk of a partial write. The method returns a refreshed FileInfo for the destination file. - GetSize(string searchPattern = ControlChars.WildcardAllFiles, SearchOption searchOption = SearchOption.TopDirectoryOnly)
Adds up the byte lengths of files matching the search pattern. Use SearchOption.AllDirectories to include subdirectories. If reading an individual file’s length throws an IOException, the method traces the error, skips that file, and continues with the remaining files. - ReadAllTextSafe(string fileName, string fallback = “”, Encoding? encoding = null)
Resolves a file name within the directory and delegates to FileInfoExtensions.ReadAllTextSafe(). It returns the supplied fallback when that read fails, giving callers a convenient value to use when the file is unavailable. - WriteAllTextAtomic(string fileName, string content, Encoding? encoding = null)
Provides a concise way to call CreateTempFileThenMove() for a file in the directory. Use it when writing text through a temporary file better suits the operation than writing directly to the destination.
Summary
DirectoryInfoExtensions removes repetitive setup from everyday file operations. Its methods help you write text through a temporary file, read with a fallback, and measure the files you select. For directory size calculations, remember that GetSize() skips IOException from individual file lengths; errors encountered while enumerating files can still reach the caller.
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.
If 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 reproduced in any way without express permission from David McCarter.
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.

