{"record":{"id":"b6f103800975b6e7","repo":"OrchardCMS/OrchardCore","slug":"cannot-create-file-path-because-it-already-exists-b6f103","errorCode":null,"errorMessage":"Cannot create file '{path}' because it already exists.","messagePattern":"Cannot create file '(.+?)' because it already exists\\.","errorType":"exception","errorClass":"FileStoreException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.FileStorage.FileSystem/FileSystemStore.cs","lineNumber":364,"sourceCode":"        catch (FileStoreException)\n        {\n            throw;\n        }\n        catch (Exception ex)\n        {\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)","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.FileStorage.FileSystem/FileSystemStore.cs#L346-L382","documentation":"FileSystemStore.CreateFileFromStreamAsync throws FileStoreException when the target store-relative path already exists on disk and the overwrite parameter is false (or when the path is an existing directory). The store refuses to silently replace files, so callers must opt in with overwrite: true or delete the existing file first.","triggerScenarios":"await fileStore.CreateFileFromStreamAsync(path, stream) with overwrite omitted/false while a file already exists at that path; also triggered when the path resolves to an existing directory (separate message variant).","commonSituations":"Re-uploading media with the same filename without enabling overwrite; duplicate upload of a migration that already ran; importing content whose filenames collide with existing media; path collision where a directory with the same name exists.","solutions":["Pass overwrite: true to CreateFileFromStreamAsync when replacing existing content is intended.","Check File.Exists-equivalent first: await fileStore.GetFileInfoAsync(path) and generate a unique name (append timestamp/GUID) if taken.","Delete the existing file explicitly (fileStore.DeleteFileAsync(path)) before recreating if overwrite semantics are not desired.","Catch FileStoreException and inform the user the filename is already in use so they can rename."],"exampleFix":"// before\nusing var stream = file.OpenReadStream();\nawait _fileStore.CreateFileFromStreamAsync(path, stream);\n\n// after\nusing var stream = file.OpenReadStream();\nawait _fileStore.CreateFileFromStreamAsync(path, stream, overwrite: true);\n// or: unique name\n// path = await EnsureUniquePathAsync(_fileStore, path);","handlingStrategy":"validation","validationCode":"var existing = await fileStore.GetFileInfoAsync(path);\nif (existing is not null && !overwriteAllowed)\n{\n    // pick a unique name or reject the upload\n    path = GenerateUniqueName(path);\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    await fileStore.CreateFileFromStreamAsync(path, stream, overwrite: false);\n}\ncatch (FileStoreException)\n{\n    return Results.Conflict($\"A file named '{path}' already exists. Choose another name or enable overwrite.\");\n}","preventionTips":["Decide overwrite semantics up front and pass overwrite explicitly rather than relying on the default false.","Generate unique filenames (GUID/timestamp suffix) for user uploads on shared media paths.","Make idempotent import/migration jobs check GetFileInfoAsync before writing.","Distinguish file-vs-directory collisions: a directory at the target path cannot be overwritten."],"tags":["file-storage","file-already-exists","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"}