{"record":{"id":"5ea8d14feb7b2782","repo":"stride3d/stride","slug":"directory-path-already-exists","errorCode":null,"errorMessage":"Directory {path} already exists.","messagePattern":"Directory (.+?) already exists\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.IO/TemporaryDirectory.cs","lineNumber":21,"sourceCode":"#if STRIDE_PLATFORM_DESKTOP\nusing System.Diagnostics;\n\nnamespace Stride.Core.IO;\n\npublic class TemporaryDirectory : IDisposable\n{\n    public TemporaryDirectory()\n        : this(Guid.NewGuid().ToString()[..8])\n    {\n    }\n\n    public TemporaryDirectory(string path)\n    {\n        DirectoryPath = Path.GetFullPath(path);\n\n        if (Directory.Exists(DirectoryPath))\n        {\n            throw new InvalidOperationException($\"Directory {path} already exists.\");\n        }\n\n        Directory.CreateDirectory(this.DirectoryPath);\n    }\n\n    public string DirectoryPath { get; }\n\n    public void Dispose()\n    {\n        DeleteDirectory(DirectoryPath);\n        GC.SuppressFinalize(this);\n    }\n\n    public static void DeleteDirectory(string directoryPath)\n    {\n        // From http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true/329502#329502\n\n        if (!Directory.Exists(directoryPath))","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.IO/TemporaryDirectory.cs#L3-L39","documentation":"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.","triggerScenarios":"new TemporaryDirectory(path) where Directory.Exists(path) is true at construction time.","commonSituations":"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.","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"],"exampleFix":"// before\nvar tmp = new TemporaryDirectory(\"C:\\\\Temp\\\\work\"); // throws if it exists\n// after\nvar path = Path.Combine(Path.GetTempPath(), \"work-\" + Guid.NewGuid().ToString(\"N\"));\nvar tmp = new TemporaryDirectory(path);","handlingStrategy":"validation","validationCode":"if (Directory.Exists(path)) Directory.Delete(path, recursive: true);\nvar tmp = new TemporaryDirectory(path);","typeGuard":null,"tryCatchPattern":"try { var tmp = new TemporaryDirectory(path); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"already exists\")) { Directory.Delete(path, true); tmp = new TemporaryDirectory(path); }","preventionTips":["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"],"tags":["io","directory","lifetime"],"backgroundTag":"file-already-exists","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}