TemporaryFile Class
Represents a temporary file that is automatically deleted when disposed.
public sealed class TemporaryFile : IAsyncDisposable, IDisposableRemarks
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
| Name | Description |
|---|---|
| FullPath | Gets the full path to the temporary file. |
FullPath
TemporaryFile.csMethods
| Name | Description |
|---|---|
| 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
Create()
TemporaryFile.csCreates 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.csCreates a new temporary file at the specified path.
public static TemporaryFile Create(FullPath filePath)Parameters
filePathFullPath- The full path where the temporary file will be created.
Returns
TemporaryFile A new TemporaryFile instance.
Exceptions
ArgumentExceptionfilePathis 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.csCreates a new temporary file using the specified file name or full path.
public static TemporaryFile Create(string fileNameOrPath)Parameters
fileNameOrPathstring- The file name or full path for the temporary file.
Returns
TemporaryFile A new TemporaryFile instance.
Exceptions
ArgumentExceptionfileNameOrPathisnull, 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.
Dispose()
TemporaryFile.csDeletes 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.csOperators
| Name | Description |
|---|---|
| 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.csImplicitly converts a TemporaryFile to a TemporaryFile.FullPath.
public static implicit operator FullPath(TemporaryFile temporaryFile)Parameters
temporaryFileTemporaryFile- The temporary file to convert.
Returns
Exceptions
ArgumentNullExceptiontemporaryFileisnull.
implicit operator FileInfo(TemporaryFile)
TemporaryFile.csImplicitly converts a TemporaryFile to a FileInfo.
public static implicit operator FileInfo(TemporaryFile temporaryFile)Parameters
temporaryFileTemporaryFile- The temporary file to convert.
Returns
Exceptions
ArgumentNullExceptiontemporaryFileisnull.
implicit operator string(TemporaryFile)
TemporaryFile.csImplicitly converts a TemporaryFile to a string path.
public static implicit operator string(TemporaryFile temporaryFile)Parameters
temporaryFileTemporaryFile- The temporary file to convert.
Returns
Exceptions
ArgumentNullExceptiontemporaryFileisnull.