Safe temporary file and directory management with automatic cleanup via IDisposable and IAsyncDisposable.
dotnet add package Philiprehberger.TempFileSafe temporary file and directory management with automatic cleanup via IDisposable and IAsyncDisposable.
dotnet add package Philiprehberger.TempFile
using Philiprehberger.TempFile;
// Create a temp file (deleted on dispose)
using var file = TempFile.Create();
Console.WriteLine(file.Path);
// Create with a specific extension
using var csvFile = TempFile.Create(".csv");
// Create with string content
using var log = TempFile.WithContent("Hello, world!");
// Create with stream content
using var copy = TempFile.WithContent(someStream);
// Create in a specific directory
using var scoped = TempFile.InDirectory("/tmp/my-app");
// Access the underlying FileStream
file.Stream.Write(data);
using Philiprehberger.TempFile;
// Create a temp directory (recursively deleted on dispose)
using var dir = TempDirectory.Create();
Console.WriteLine(dir.Path);
// Create with a prefix
using var prefixed = TempDirectory.Create("myapp-");
await using var file = TempFile.Create();
await using var dir = TempDirectory.Create();
TempFile| Member | Description |
|---|---|
Create() | Creates a temp file with a random name |
Create(extension) | Creates a temp file with the specified extension |
WithContent(string) | Creates a temp file with string content |
WithContent(Stream) | Creates a temp file with content from a stream |
InDirectory(dir) | Creates a temp file in the specified directory |
Path | Full path of the temporary file |
FileName | File name without directory |
Stream | Lazily-opened FileStream for reading and writing |
TempDirectory| Member | Description |
|---|---|
Create() | Creates a temp directory with a random name |
Create(prefix) | Creates a temp directory with a prefix |
Path | Full path of the temporary directory |
DirectoryName | Directory name (last path segment) |
TempFileOptions| Property | Description |
|---|---|
Extension | File extension (e.g., ".txt") |
Directory | Target directory for the temp file |
Prefix | Prefix for the file name |
dotnet build src/Philiprehberger.TempFile.csproj --configuration Release
If you find this project useful: