{"record":{"id":"3cf23e6965461467","repo":"OrchardCMS/OrchardCore","slug":"cannot-create-file-path-because-it-already-exists-as-a","errorCode":null,"errorMessage":"Cannot create file '{path}' because it already exists as a directory.","messagePattern":"Cannot create file '(.+?)' because it already exists as a directory\\.","errorType":"exception","errorClass":"FileStoreException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.FileStorage.FileSystem/FileSystemStore.cs","lineNumber":369,"sourceCode":"        {\n            throw new FileStoreException($\"Cannot get file stream of the file '{fileStoreEntry.Path}'.\", ex);\n        }\n    }\n\n    public async Task<string> CreateFileFromStreamAsync(string path, Stream inputStream, bool overwrite = false)\n    {\n        try\n        {\n            var physicalPath = GetPhysicalPath(path);\n\n            if (!overwrite && File.Exists(physicalPath))\n            {\n                throw new FileStoreException($\"Cannot create file '{path}' because it already exists.\");\n            }\n\n            if (Directory.Exists(physicalPath))\n            {\n                throw new FileStoreException($\"Cannot create file '{path}' because it already exists as a directory.\");\n            }\n\n            // Create directory path if it doesn't exist.\n            var physicalDirectoryPath = Path.GetDirectoryName(physicalPath);\n            Directory.CreateDirectory(physicalDirectoryPath);\n\n            var fileInfo = new FileInfo(physicalPath);\n            await using var outputStream = fileInfo.Create();\n            await inputStream.CopyToAsync(outputStream);\n\n            return path;\n        }\n        catch (FileStoreException)\n        {\n            throw;\n        }\n        catch (Exception ex)\n        {","sourceCodeStart":351,"sourceCodeEnd":387,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.FileStorage.FileSystem/FileSystemStore.cs#L351-L387","documentation":"FileSystemStore.CreateFileFromStreamAsync throws FileStoreException when the target path already exists on disk as a directory. A file cannot be written over a directory, so the store fails fast before touching the filesystem. This is a pre-write existence guard distinct from the file-already-exists check just above it.","triggerScenarios":"Calling IFileStore.CreateFileFromStreamAsync(path, stream) where Path 'path' (or its first segment) names an existing directory under the store root, e.g. path = 'media/uploads' while 'media/uploads' is a directory created by CreateDirectoryFromPathAsync.","commonSituations":"Path-segment confusion: storing 'foo/bar.txt' after a directory 'foo' was created at the wrong level; treating a folder path as a file path when building media URLs; migrations that created directories from file names (trailing slash confusion or path separator bugs on case-insensitive filesystems).","solutions":["Check the target with IFileStore.GetDirectoryInfoAsync(path) (or Directory.Exists on the physical path) before writing; if it is a directory, choose a different filename or remove the directory.","Correct the calling code so the path points to a file name, not a folder (append the intended filename to the folder path).","If the directory is a leftover artifact, delete it via IFileStore.TryDeleteDirectoryAsync and retry the create.","Wrap the call in try/catch on FileStoreException to surface a clear message to the user instead of an unhandled exception."],"exampleFix":"// before\nawait fileStore.CreateFileFromStreamAsync(\"media/uploads\", stream);\n// after\nvar target = \"media/uploads/report.pdf\";\nif (await fileStore.GetDirectoryInfoAsync(target) is not null)\n{\n    throw new InvalidOperationException($\"'{target}' is an existing directory; choose a file path.\");\n}\nawait fileStore.CreateFileFromStreamAsync(target, stream);","handlingStrategy":"validation","validationCode":"if (await fileStore.GetDirectoryInfoAsync(path) is not null)\n{\n    throw new InvalidOperationException($\"'{path}' is an existing directory; cannot create a file there.\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    await fileStore.CreateFileFromStreamAsync(path, stream);\n}\ncatch (FileStoreException ex) when (ex.Message.Contains(\"as a directory\"))\n{\n    logger.LogWarning(\"Target '{Path}' is a directory; adjust the path.\", path);\n}","preventionTips":["Always compose file paths as folder + filename; never pass a folder path to CreateFileFromStreamAsync.","Check GetFileInfoAsync/GetDirectoryInfoAsync before creating, especially with user-supplied paths.","Keep a naming convention that separates directory segments from file names in media URLs."],"tags":["file-storage","path-conflict","filesystem"],"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"}