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
Introducing the TempFileManager
The TempFileManager is a powerful utility designed to create and manage temporary files efficiently. It simplifies file management for various use cases, such as unit testing, benchmarking, and work-related tasks.
Automatic Cleanup with IDisposable
TempFileManager implements IDisposable, ensuring that all managed temporary files are automatically deleted when the instance is disposed. This helps free system resources efficiently and prevents orphaned files from accumulating.
Key Features and Methods
The TempFileManager provides several essential methods to streamline temporary file creation and cleanup:
- CreateFile() – Generates a new temporary file and returns its full path.
- CreateFiles(int count) – Creates a specified number of temporary files and returns their paths in a ReadOnlyCollection<string>.
- DeleteAllFiles() – Deletes all temporary files managed by the instance, ensuring a complete cleanup.
- DeleteFile(string fileName) – Removes a specific temporary file by name, allowing precise control over file management.
- GetManagedFiles() – Returns a ReadOnlyCollection<string> containing all currently managed files for inspection or further processing.
Example Usage
Here is a simple example demonstrating how to use TempFileManager:
using (var manager = new TempFileManager())
{
var files = manager.CreateFiles(50);
foreach (var file in files)
{
// Access or modify the file
}
}
Best Practices
- Always use
TempFileManagerwithin ausingstatement to ensure proper disposal. - If you use
TempFileManageras a class-level field, implementIDisposablein your class to ensure cleanup.
Why Use TempFileManager?
This utility is an invaluable tool for projects that require temporary file handling, especially in automated tests where frequent file creation and cleanup are necessary. By integrating TempFileManager, you can streamline temporary file management, reduce resource waste, and enhance the reliability of your applications.
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.

