TemporaryDirectory Class

Represents a temporary directory that is automatically deleted when disposed.

public sealed class TemporaryDirectory : IAsyncDisposable, IDisposable

Remarks

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

NameDescription
FullPathGets the full path to the temporary directory.

Gets 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

NameDescription
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

Creates 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.cs

Creates a new temporary directory under the specified root directory.

public static TemporaryDirectory Create(FullPath rootDirectory)
Parameters
rootDirectory FullPath
The root directory where the temporary directory will be created.
Returns

TemporaryDirectory A new TemporaryDirectory instance.

Exceptions
UnauthorizedAccessException
rootDirectory already 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.cs

Creates a directory at the specified relative path.

public FullPath CreateDirectory(string relativePath)

Parameters

relativePath string
The relative path for the directory (can include subdirectories).

Returns

FullPath The absolute TemporaryDirectory.FullPath of the created directory.

Exceptions

ArgumentException
relativePath resolves 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.cs

Creates an empty file at the specified relative path.

public FullPath CreateEmptyFile(string relativePath)

Parameters

relativePath string
The relative path for the file (can include subdirectories).

Returns

FullPath The absolute TemporaryDirectory.FullPath of the created file.

Exceptions

ArgumentException
relativePath resolves 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.cs

Creates a text file with the specified content at the relative path.

public FullPath CreateTextFile(string relativePath, string content)

Parameters

relativePath string
The relative path for the file (can include subdirectories).
content string
The text content to write to the file.

Returns

FullPath The absolute TemporaryDirectory.FullPath of the created file.

Exceptions

ArgumentException
relativePath resolves 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.cs

Asynchronously 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

relativePath string
The relative path for the file (can include subdirectories).
content string
The text content to write to the file.
cancellationToken CancellationToken
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

ArgumentException
relativePath resolves 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));

Deletes 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.cs

Asynchronously 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.cs

Gets the full path for a relative path within the temporary directory.

public FullPath GetFullPath(string relativePath)

Parameters

relativePath string
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.
ArgumentException
relativePath is 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

NameDescription
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.cs

Combines the temporary directory path with a relative path using the / operator.

public static FullPath operator /(TemporaryDirectory temporaryDirectory, string path)

Parameters

temporaryDirectory TemporaryDirectory
The temporary directory.
path string
The relative path to combine.

Returns

FullPath The combined TemporaryDirectory.FullPath.

Exceptions

ArgumentNullException
temporaryDirectory is null.

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.cs
public static implicit operator FullPath(TemporaryDirectory temporaryDirectory)
Parameters
temporaryDirectory TemporaryDirectory
The temporary directory to convert.
Returns

FullPath

Exceptions
ArgumentNullException
temporaryDirectory is null.

implicit operator DirectoryInfo(TemporaryDirectory)

TemporaryDirectory.cs

Implicitly converts a TemporaryDirectory to a DirectoryInfo.

public static implicit operator DirectoryInfo(TemporaryDirectory temporaryDirectory)
Parameters
temporaryDirectory TemporaryDirectory
The temporary directory to convert.
Returns

DirectoryInfo

Exceptions
ArgumentNullException
temporaryDirectory is null.

implicit operator string(TemporaryDirectory)

TemporaryDirectory.cs

Implicitly converts a TemporaryDirectory to a string path.

public static implicit operator string(TemporaryDirectory temporaryDirectory)
Parameters
temporaryDirectory TemporaryDirectory
The temporary directory to convert.
Returns

string

Exceptions
ArgumentNullException
temporaryDirectory is null.