TemporaryDirectory Class
Represents a temporary directory that is automatically deleted when disposed.
public sealed class TemporaryDirectory : IAsyncDisposable, IDisposableRemarks
This class creates a unique temporary directory that is automatically cleaned up when disposed. Uniqueness is guaranteed by including a GUID in the directory name.
Example
// Basic usage with using statement
using var tempDir = TemporaryDirectory.Create();
var filePath = tempDir.CreateTextFile("test.txt", "Hello World");
Assert.Equal("Hello World", File.ReadAllText(filePath));
// Directory is automatically deleted when disposed
// Async disposal
await using var tempDir2 = TemporaryDirectory.Create();
await tempDir2.CreateTextFileAsync("data.json", "{}");
// Working with subdirectories
using var tempDir3 = TemporaryDirectory.Create();
tempDir3.CreateDirectory("subdir");
var nestedFile = tempDir3.GetFullPath("subdir/file.txt");
File.WriteAllText(nestedFile, "content");
// Using path operators
using var tempDir4 = TemporaryDirectory.Create();
var path = tempDir4 / "data" / "output.txt";
Assert.Equal(tempDir4.GetFullPath("data/output.txt"), path);- Implements
Properties
| Name | Description |
|---|---|
| FullPath | Gets the full path to the temporary directory. |
FullPath
TemporaryDirectory.csGets the full path to the temporary directory.
public FullPath FullPath { get; }Property value
FullPath The absolute path to the temporary directory where files and subdirectories can be created.
Remarks
The path remains readable after the instance is disposed, even though the directory no longer exists.
Methods
| Name | Description |
|---|---|
| Create() | Creates a new temporary directory in the system's default temp location. |
| Create(FullPath) | Creates a new temporary directory under the specified root directory. |
| CreateDirectory(string) | Creates a directory at the specified relative path. |
| CreateEmptyFile(string) | Creates an empty file at the specified relative path. |
| CreateTextFile(string, string) | Creates a text file with the specified content at the relative path. |
| CreateTextFileAsync(string, string, CancellationToken) | Asynchronously creates a text file with the specified content at the relative path. |
| Dispose() | Deletes the temporary directory and all its contents. |
| DisposeAsync() | Asynchronously deletes the temporary directory and all its contents. |
| GetFullPath(string) | Gets the full path for a relative path within the temporary directory. |
| OpenInExplorer() | Opens the temporary directory in Windows Explorer. |
Create overloads
Create()
TemporaryDirectory.csCreates a new temporary directory in the system's default temp location.
public static TemporaryDirectory Create()Returns
TemporaryDirectory A new TemporaryDirectory instance.
Exceptions
UnauthorizedAccessException- The "MezTD" directory already exists and is accessible to other users.
Remarks
The directory is created under the system temp path in a "MezTD" subdirectory. The directory name includes a timestamp and GUID to ensure uniqueness. On Unix, the directories are created so that only the current user can access them.
Create(FullPath)
TemporaryDirectory.csCreates a new temporary directory under the specified root directory.
public static TemporaryDirectory Create(FullPath rootDirectory)Parameters
rootDirectoryFullPath- The root directory where the temporary directory will be created.
Returns
TemporaryDirectory A new TemporaryDirectory instance.
Exceptions
UnauthorizedAccessExceptionrootDirectoryalready exists and is accessible to other users.
Remarks
The directory name includes a timestamp (yyyyMMdd_HHmmss) and GUID to ensure uniqueness. On Unix, the directories are created so that only the current user can access them.
CreateDirectory(string)
TemporaryDirectory.csCreates a directory at the specified relative path.
Parameters
relativePathstring- The relative path for the directory (can include subdirectories).
Returns
FullPath The absolute TemporaryDirectory.FullPath of the created directory.
Exceptions
ArgumentExceptionrelativePathresolves to a path outside the temporary directory.
Remarks
If parent directories don't exist, they will be created automatically.
Example
using var tempDir = TemporaryDirectory.Create();
var dirPath = tempDir.CreateDirectory("data/output");
Assert.True(Directory.Exists(dirPath));CreateEmptyFile(string)
TemporaryDirectory.csCreates an empty file at the specified relative path.
Parameters
relativePathstring- The relative path for the file (can include subdirectories).
Returns
FullPath The absolute TemporaryDirectory.FullPath of the created file.
Exceptions
ArgumentExceptionrelativePathresolves to a path outside the temporary directory.
Remarks
If the parent directories don't exist, they will be created automatically.
Example
using var tempDir = TemporaryDirectory.Create();
var filePath = tempDir.CreateEmptyFile("test.txt");
Assert.Equal(0, new FileInfo(filePath).Length);CreateTextFile(string, string)
TemporaryDirectory.csCreates a text file with the specified content at the relative path.
Parameters
relativePathstring- The relative path for the file (can include subdirectories).
contentstring- The text content to write to the file.
Returns
FullPath The absolute TemporaryDirectory.FullPath of the created file.
Exceptions
ArgumentExceptionrelativePathresolves to a path outside the temporary directory.
Remarks
If the parent directories don't exist, they will be created automatically.
Example
using var tempDir = TemporaryDirectory.Create();
var filePath = tempDir.CreateTextFile("config.json", "{ \"setting\": \"value\" }");
Assert.Equal("{ \"setting\": \"value\" }", File.ReadAllText(filePath));CreateTextFileAsync(string, string, CancellationToken)
TemporaryDirectory.csAsynchronously creates a text file with the specified content at the relative path.
net10.0
public Task<FullPath> CreateTextFileAsync(string relativePath, string content, CancellationToken cancellationToken = default)net11.0
public Task<FullPath> CreateTextFileAsync(string relativePath, string content, CancellationToken cancellationToken = default)Parameters
relativePathstring- The relative path for the file (can include subdirectories).
contentstring- The text content to write to the file.
cancellationTokenCancellationToken- A cancellation token to cancel the operation.
Returns
Task<FullPath> A task that represents the asynchronous operation, containing the absolute TemporaryDirectory.FullPath of the created file.
Exceptions
ArgumentExceptionrelativePathresolves to a path outside the temporary directory.
Remarks
If the parent directories don't exist, they will be created automatically.
Example
await using var tempDir = TemporaryDirectory.Create();
var filePath = await tempDir.CreateTextFileAsync("data.txt", "Hello World");
Assert.Equal("Hello World", await File.ReadAllTextAsync(filePath));Dispose()
TemporaryDirectory.csDeletes the temporary directory and all its contents.
public void Dispose()Remarks
Disposing an already disposed instance does nothing, so it cannot delete a directory recreated at the same path.
DisposeAsync()
TemporaryDirectory.csAsynchronously deletes the temporary directory and all its contents.
net10.0
public ValueTask DisposeAsync()net11.0
public ValueTask DisposeAsync()Returns
ValueTask A task that represents the asynchronous dispose operation.
Remarks
Disposing an already disposed instance does nothing, so it cannot delete a directory recreated at the same path.
GetFullPath(string)
TemporaryDirectory.csGets the full path for a relative path within the temporary directory.
Parameters
relativePathstring- The relative path (can include subdirectories).
Returns
FullPath The absolute TemporaryDirectory.FullPath combining the temporary directory and the relative path.
Exceptions
ObjectDisposedException- The instance has been disposed.
ArgumentExceptionrelativePathis rooted, or resolves to a path outside the temporary directory.
Remarks
The resulting path must stay inside the temporary directory, so it cannot be escaped using "..".
Example
using var tempDir = TemporaryDirectory.Create();
var fullPath = tempDir.GetFullPath("data/output.txt");
Assert.Equal(tempDir.FullPath / "data" / "output.txt", fullPath);OpenInExplorer()
TemporaryDirectory.cs- Supported on windows5.1.2600
Opens the temporary directory in Windows Explorer.
[SupportedOSPlatform("windows5.1.2600")]
public void OpenInExplorer()Exceptions
ObjectDisposedException- The instance has been disposed.
Remarks
This method is only available on Windows platforms.
Operators
| Name | Description |
|---|---|
| operator /(TemporaryDirectory, string) | Combines the temporary directory path with a relative path using the / operator. |
| implicit operator FullPath(TemporaryDirectory) | Implicitly converts a TemporaryDirectory to a TemporaryDirectory.FullPath. |
| implicit operator DirectoryInfo(TemporaryDirectory) | Implicitly converts a TemporaryDirectory to a DirectoryInfo. |
| implicit operator string(TemporaryDirectory) | Implicitly converts a TemporaryDirectory to a string path. |
operator /(TemporaryDirectory, string)
TemporaryDirectory.csCombines the temporary directory path with a relative path using the / operator.
public static FullPath operator /(TemporaryDirectory temporaryDirectory, string path)Parameters
temporaryDirectoryTemporaryDirectory- The temporary directory.
pathstring- The relative path to combine.
Returns
FullPath The combined TemporaryDirectory.FullPath.
Exceptions
ArgumentNullExceptiontemporaryDirectoryisnull.
Example
using var tempDir = TemporaryDirectory.Create();
var filePath = tempDir / "data" / "output.txt";
Assert.Equal(tempDir.GetFullPath("data/output.txt"), filePath);op_Implicit overloads
implicit operator FullPath(TemporaryDirectory)
TemporaryDirectory.csImplicitly converts a TemporaryDirectory to a TemporaryDirectory.FullPath.
public static implicit operator FullPath(TemporaryDirectory temporaryDirectory)Parameters
temporaryDirectoryTemporaryDirectory- The temporary directory to convert.
Returns
Exceptions
ArgumentNullExceptiontemporaryDirectoryisnull.
implicit operator DirectoryInfo(TemporaryDirectory)
TemporaryDirectory.csImplicitly converts a TemporaryDirectory to a DirectoryInfo.
public static implicit operator DirectoryInfo(TemporaryDirectory temporaryDirectory)Parameters
temporaryDirectoryTemporaryDirectory- The temporary directory to convert.
Returns
Exceptions
ArgumentNullExceptiontemporaryDirectoryisnull.
implicit operator string(TemporaryDirectory)
TemporaryDirectory.csImplicitly converts a TemporaryDirectory to a string path.
public static implicit operator string(TemporaryDirectory temporaryDirectory)Parameters
temporaryDirectoryTemporaryDirectory- The temporary directory to convert.
Returns
Exceptions
ArgumentNullExceptiontemporaryDirectoryisnull.