TemporaryFile Class

Represents a temporary file that is automatically deleted when disposed.

public sealed class TemporaryFile : IAsyncDisposable, IDisposable

Remarks

This class creates a unique temporary file that is automatically cleaned up when disposed. When providing a file name, the file is created under the system temp path in a "MezTF" subdirectory.

Example

// Basic usage with using statement
using var tempFile = TemporaryFile.Create();
File.WriteAllText(tempFile, "content");

// Provide a file name
using var tempFile2 = TemporaryFile.Create("custom.txt");
Assert.Equal("custom.txt", tempFile2.FullPath.Name);

// Provide a full path. The file must not exist.
using var tempFile3 = TemporaryFile.Create(Path.Combine(Path.GetTempPath(), $"custom-{Guid.NewGuid():N}.txt"));
Implements

Properties

NameDescription
FullPathGets the full path to the temporary file.

Gets the full path to the temporary file.

public FullPath FullPath { get; }

Property value

FullPath The absolute path to the temporary file.

Methods

NameDescription
Create()Creates a new temporary file in the system's default temp location.
Create(FullPath)Creates a new temporary file at the specified path.
Create(string)Creates a new temporary file using the specified file name or full path.
Dispose()Deletes the temporary file.
DisposeAsync()Asynchronously deletes the temporary file.

Create overloads

Creates a new temporary file in the system's default temp location.

public static TemporaryFile Create()
Returns

TemporaryFile A new TemporaryFile instance.

Remarks

The file is created under the system temp path in a "MezTF" subdirectory. The file name includes a timestamp and GUID to ensure uniqueness.

Create(FullPath)

TemporaryFile.cs

Creates a new temporary file at the specified path.

public static TemporaryFile Create(FullPath filePath)
Parameters
filePath FullPath
The full path where the temporary file will be created.
Returns

TemporaryFile A new TemporaryFile instance.

Exceptions
ArgumentException
filePath is empty.
IOException
The file already exists.
Remarks

The file must not already exist. An existing file is never adopted, so disposing the returned instance cannot delete data that it did not create.

Create(string)

TemporaryFile.cs

Creates a new temporary file using the specified file name or full path.

public static TemporaryFile Create(string fileNameOrPath)
Parameters
fileNameOrPath string
The file name or full path for the temporary file.
Returns

TemporaryFile A new TemporaryFile instance.

Exceptions
ArgumentException
fileNameOrPath is null, empty, or contains only white-space characters.
IOException
The file already exists.
Remarks

If fileNameOrPath is not rooted, it is created under the system temp path in a "MezTF" subdirectory.

Deletes the temporary file.

public void Dispose()

Remarks

When the instance was created from a file name, the unique directory created to hold that file is deleted as well.

DisposeAsync()

TemporaryFile.cs

Asynchronously deletes the temporary file.

public ValueTask DisposeAsync()

Returns

ValueTask A task that represents the asynchronous dispose operation.

Remarks

When the instance was created from a file name, the unique directory created to hold that file is deleted as well.

Operators

NameDescription
implicit operator FullPath(TemporaryFile)Implicitly converts a TemporaryFile to a TemporaryFile.FullPath.
implicit operator FileInfo(TemporaryFile)Implicitly converts a TemporaryFile to a FileInfo.
implicit operator string(TemporaryFile)Implicitly converts a TemporaryFile to a string path.

op_Implicit overloads

implicit operator FullPath(TemporaryFile)

TemporaryFile.cs

Implicitly converts a TemporaryFile to a TemporaryFile.FullPath.

public static implicit operator FullPath(TemporaryFile temporaryFile)
Parameters
temporaryFile TemporaryFile
The temporary file to convert.
Returns

FullPath

Exceptions
ArgumentNullException
temporaryFile is null.

implicit operator FileInfo(TemporaryFile)

TemporaryFile.cs

Implicitly converts a TemporaryFile to a FileInfo.

public static implicit operator FileInfo(TemporaryFile temporaryFile)
Parameters
temporaryFile TemporaryFile
The temporary file to convert.
Returns

FileInfo

Exceptions
ArgumentNullException
temporaryFile is null.

implicit operator string(TemporaryFile)

TemporaryFile.cs

Implicitly converts a TemporaryFile to a string path.

public static implicit operator string(TemporaryFile temporaryFile)
Parameters
temporaryFile TemporaryFile
The temporary file to convert.
Returns

string

Exceptions
ArgumentNullException
temporaryFile is null.