{"record":{"id":"a35dfde8124ae6d5","repo":"OrchardCMS/OrchardCore","slug":"cannot-create-directory-because-the-path-path-already-exists-a35dfd","errorCode":null,"errorMessage":"Cannot create directory because the path '{path}' already exists and is a file.","messagePattern":"Cannot create directory because the path '(.+?)' already exists and is a file\\.","errorType":"exception","errorClass":"FileStoreException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.FileStorage.FileSystem/FileSystemStore.cs","lineNumber":179,"sourceCode":"                });\n\n            return results.ToAsyncEnumerable();\n        }\n        catch (Exception ex)\n        {\n            throw new FileStoreException($\"Cannot get directories with path '{path}'.\", ex);\n        }\n    }\n\n    public Task<bool> TryCreateDirectoryAsync(string path)\n    {\n        try\n        {\n            var physicalPath = GetPhysicalPath(path);\n\n            if (File.Exists(physicalPath))\n            {\n                throw new FileStoreException($\"Cannot create directory because the path '{path}' already exists and is a file.\");\n            }\n\n            if (Directory.Exists(physicalPath))\n            {\n                return Task.FromResult(false);\n            }\n\n            Directory.CreateDirectory(physicalPath);\n\n            return Task.FromResult(true);\n        }\n        catch (FileStoreException)\n        {\n            throw;\n        }\n        catch (Exception ex)\n        {\n            throw new FileStoreException($\"Cannot create directory '{path}'.\", ex);","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.FileStorage.FileSystem/FileSystemStore.cs#L161-L197","documentation":"FileSystemStore wraps the physical file system and throws FileStoreException for store-level failures. This error is thrown when TryCreateDirectoryAsync is asked to create a directory at a virtual path that maps to an existing physical FILE. The store refuses to overwrite a file with a directory rather than corrupting the store.","triggerScenarios":"Calling TryCreateDirectoryAsync(path) where GetPhysicalPath(path) points to an existing file, e.g. a media file was previously stored at 'folder' (no extension) and code later calls TryCreateDirectoryAsync(\"folder\").","commonSituations":"Path casing/extension mistakes in media library code; importing content that assumes 'folder' is a directory when a file of that name exists; race between file upload and directory creation on the same path.","solutions":["Check with await store.GetFileInfoAsync(path) (or File.Exists on the physical path) before creating the directory and delete/rename the conflicting file first.","Use a different path for the directory.","Ensure callers never pass file paths to directory-creation APIs."],"exampleFix":"// before\nawait store.TryCreateDirectoryAsync(\"uploads/logo\"); // 'logo' is an existing file\n// after\nvar existing = await store.GetFileInfoAsync(\"uploads/logo\");\nif (existing is not null) { await store.TryDeleteFileAsync(\"uploads/logo\"); }\nawait store.TryCreateDirectoryAsync(\"uploads/logo\");","handlingStrategy":"validation","validationCode":"var dirInfo = await store.GetDirectoryInfoAsync(path);\nvar fileInfo = await store.GetFileInfoAsync(path);\nif (fileInfo is not null) throw new InvalidOperationException($\"'{path}' is a file, not a directory\");\nif (dirInfo is not null) return; // already exists","typeGuard":"static bool IsUsableDirectoryPath(IFileStoreEntry entry) => entry is not null && entry.IsDirectory;","tryCatchPattern":"try { await store.TryCreateDirectoryAsync(path); }\ncatch (FileStoreException ex) when (ex.Message.Contains(\"already exists and is a file\"))\n{\n    // resolve the file/dir conflict before retrying\n}","preventionTips":["Never use a path for both a file and a directory in the same store.","Check GetFileInfoAsync before creating directories from user input.","Keep directory names distinct from uploaded file names (e.g. require extensions on files)."],"tags":["filesystem","file-store","path-conflict"],"backgroundTag":"file-already-exists","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}