This is the twenty first post in my ongoing series covering the abstractions in Sasa. Previous posts:
- Sasa.Parsing - type-safe, extensible lexing and parsing framework
- Sasa.Dynamics - type-safe polytypic/reflective programming
- Sasa.Func - Type-Safe Delegate Combinators
- Sasa.Option - Handling Optional Values
- Sasa.Result - Handling Exceptional Values
- Sasa.Numbers - Generic Number Extensions
- Sasa.Strings - General String Extensions
- Sasa.Types - Runtime Types And CLR Metadata
- Sasa.Weak - Typed Weak References
- Sasa's Tuples
- Sasa's Core Interfaces
- Sasa.Events - Type-Safe, Null-Safe, Thread-Safe Events
- Sasa.Web.Url64 - URL-Safe Base64 Encoding
- Sasa.Operators<T> - Generic Arithmetic and Logical Operators
- Sasa.IO.FilePath - Easy and Safe Path Manipulations
- Sasa.IO.Streams - Convenient Stream Extensions
- Sasa.Linq.Enumerables - Extensions on IEnumerable<T>
- Sasa.Either - Simple Sums for .NET
- Sasa.Atomics - Simpler, More Scalable Atomic Operations
- Sasa.Collections.Arrays - Purely Functional Array Combinators
Sasa.IO.DisposableFile is a simple object intended to automate the handling of temporary files. Like most IDisposable objects, a DisposableFile is intended to be used within a "using" block, and the file it references is deleted upon exiting the block:
using (var tmp = new DisposableFile(true, "foo.txt")) { File.WriteAllText(tmp.Path, "hello world!"); Console.WriteLine(File.ReadAllText(tmp.Path)); } Console.WriteLine(File.Exists("foo.txt")); // output: // hello world! // false
Any FileNotFoundException's thrown on dispose are ignored, so you can safely move the file within the "using" block.
Sasa.IO.DisposableFile Constructor
The DisposableFile constructor creates a temporary file:
using (var tmp = new DisposableFile(createIfNotExists: true, path: "foo.txt")) { ... }
Sasa.IO.DisposableFile.Path
The Path property exposes the path to the disposable file:
using (var tmp = new DisposableFile(createIfNotExists: true, path: "foo.txt")) { Console.WriteLine(tmp.Path); } // output: // foo.txt
Sasa.IO.DisposableFile.CreateTemporary
The CreateTemporary static method invokes System.IO.Path.GetTempFileName and returns a DisposableFile instance encapsulating the created file:
using (var tmp = DisposableFile.CreateTemporary()) { File.WriteAllText(tmp.Path, "hello world!"); }
Comments