stride3d/stride · error · InvalidOperationException
Directory already exists.
Error message
Directory {path} already exists. What it means
The TemporaryDirectory constructor throws InvalidOperationException if the target directory already exists, to avoid clobbering or mixing files into a pre-existing directory. It creates the directory only when the path is free.
Solutions
- Delete the pre-existing directory before constructing
- Use Path.GetTempFileName()/Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()) to get a unique path
- Add a unique suffix (timestamp/GUID) to the directory name
- Add cleanup in a finally/dispose so stale dirs do not persist
Example fix
// before
var tmp = new TemporaryDirectory("C:\\Temp\\work"); // throws if it exists
// after
var path = Path.Combine(Path.GetTempPath(), "work-" + Guid.NewGuid().ToString("N"));
var tmp = new TemporaryDirectory(path); Defensive patterns
Strategy: validation
Validate before calling
if (Directory.Exists(path)) Directory.Delete(path, recursive: true); var tmp = new TemporaryDirectory(path);
Try / catch
try { var tmp = new TemporaryDirectory(path); } catch (InvalidOperationException ex) when (ex.Message.Contains("already exists")) { Directory.Delete(path, true); tmp = new TemporaryDirectory(path); } Prevention
- Use GUID/timestamp-based unique temp paths
- Always dispose/clean up temporary directories in finally
- Avoid hardcoded shared temp paths
- Handle stale dirs from crashed runs at startup
When it happens
Trigger: new TemporaryDirectory(path) where Directory.Exists(path) is true at construction time.
Common situations: Re-running a process without cleaning up the previous temporary directory, hardcoded temp paths shared by two instances, or stale directories left by a crashed run.
Understand the failure class
Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.
Related errors
- Unable to create directory
- A file path cannot end with with directory char '\' or '/'…
- Base path must be absolute, got
- Base path must be non-empty (use null for passthrough).
- Build manifest [ ] must exist
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/5ea8d14feb7b2782.
Report an issue: GitHub.
Appendix: source
Thrown at sources/core/Stride.Core.IO/TemporaryDirectory.cs:21
#if STRIDE_PLATFORM_DESKTOP
using System.Diagnostics;
namespace Stride.Core.IO;
public class TemporaryDirectory : IDisposable
{
public TemporaryDirectory()
: this(Guid.NewGuid().ToString()[..8])
{
}
public TemporaryDirectory(string path)
{
DirectoryPath = Path.GetFullPath(path);
if (Directory.Exists(DirectoryPath))
{
throw new InvalidOperationException($"Directory {path} already exists.");
}
Directory.CreateDirectory(this.DirectoryPath);
}
public string DirectoryPath { get; }
public void Dispose()
{
DeleteDirectory(DirectoryPath);
GC.SuppressFinalize(this);
}
public static void DeleteDirectory(string directoryPath)
{
// From http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true/329502#329502
if (!Directory.Exists(directoryPath))View on GitHub (pinned to 96fad776d2)