{"record":{"id":"56779515cc84e263","repo":"OrchardCMS/OrchardCore","slug":"cannot-create-directory-because-the-path-path-already-exists","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.AzureBlob/BlobFileStore.cs","lineNumber":442,"sourceCode":"            {\n                return await TryCreateDataLakeDirectoryAsync(path);\n            }\n            catch (Exception ex)\n            {\n                throw new FileStoreException($\"Cannot create directory '{path}'.\", ex);\n            }\n        }\n\n        // Since directories are only created implicitly when creating blobs, we\n        // simply pretend like we created the directory, unless there is already\n        // a blob with the same path.\n        try\n        {\n            var blobFile = GetBlobReference(path);\n\n            if (await blobFile.ExistsAsync())\n            {\n                throw new FileStoreException($\"Cannot create directory because the path '{path}' already exists and is a file.\");\n            }\n\n            var blobDirectory = await GetBlobDirectoryReference(path);\n            if (blobDirectory == null)\n            {\n                await CreateDirectoryAsync(path);\n            }\n\n            return true;\n        }\n        catch (FileStoreException)\n        {\n            throw;\n        }\n        catch (Exception ex)\n        {\n            throw new FileStoreException($\"Cannot create directory '{path}'.\", ex);\n        }","sourceCodeStart":424,"sourceCodeEnd":460,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.FileStorage.AzureBlob/BlobFileStore.cs#L424-L460","documentation":"BlobFileStore.TryCreateDirectoryAsync (flat-namespace path) checks whether a blob already exists at the requested path; because blob 'directories' are only implied by prefixes, a real blob at that exact path means the directory cannot be created. The store throws FileStoreException with this message naming the conflicting path.","triggerScenarios":"Calling TryCreateDirectoryAsync(path) on a flat (non-HNS) account where a blob was previously uploaded with exactly that key — e.g. a file named 'docs' exists and code then tries TryCreateDirectoryAsync(\"docs\").","commonSituations":"Uploading files without extensions that later collide with intended directory names; older uploads that used the path as a file key; migrations from other stores where keys were used both as files and folders; user-driven naming where a folder shares a name with an uploaded file.","solutions":["Choose a different directory name, or delete the conflicting blob first (TryDeleteFileAsync at that path).","Treat it like a file-exists condition in app logic: check GetFileInfoAsync(path) before creating a directory.","Adopt naming conventions that keep file keys and directory prefixes disjoint (e.g. always give files extensions).","If the collision was a mistake, re-upload the blob under a proper file path and retry the directory creation."],"exampleFix":"// before\nif (await fileStore.GetFileInfoAsync(path) != null)\n{\n    throw new InvalidOperationException(\"Path in use\");\n}\nawait fileStore.TryCreateDirectoryAsync(path);\n// after\nif (await fileStore.GetFileInfoAsync(path) != null)\n{\n    path = path + \"-folder\"; // avoid blob-file collision\n}\nawait fileStore.TryCreateDirectoryAsync(path);","handlingStrategy":"validation","validationCode":"var existingFile = await fileStore.GetFileInfoAsync(path);\nif (existingFile != null)\n{\n    // a blob exists at this exact key — pick another name or delete it first\n    return false;\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var created = await fileStore.TryCreateDirectoryAsync(path);\n}\ncatch (FileStoreException ex) when (ex.Message.Contains(\"already exists and is a file\"))\n{\n    // path collides with an existing blob: rename or delete the blob\n    await fileStore.TryDeleteFileAsync(path);\n    await fileStore.TryCreateDirectoryAsync(path);\n}","preventionTips":["Keep file keys and directory prefixes disjoint (e.g. require extensions on files).","Check GetFileInfoAsync before creating a directory with a user-supplied name.","Audit legacy content for blobs that double as would-be folder names.","Surface a clear UI message when a user names a folder identically to an existing file."],"tags":["azure","blob-storage","file-storage","path-collision","file-already-exists"],"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"}