OrchardCMS/OrchardCore · error · FileStoreException
Cannot create file ' ' because it already exists.
Error message
Cannot create file '{path}' because it already exists. What it means
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.
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.
Example fix
// before using var stream = file.OpenReadStream(); await _fileStore.CreateFileFromStreamAsync(path, stream); // after using var stream = file.OpenReadStream(); await _fileStore.CreateFileFromStreamAsync(path, stream, overwrite: true); // or: unique name // path = await EnsureUniquePathAsync(_fileStore, path);
Defensive patterns
Strategy: validation
Validate before calling
var existing = await fileStore.GetFileInfoAsync(path);
if (existing is not null && !overwriteAllowed)
{
// pick a unique name or reject the upload
path = GenerateUniqueName(path);
} Try / catch
try
{
await fileStore.CreateFileFromStreamAsync(path, stream, overwrite: false);
}
catch (FileStoreException)
{
return Results.Conflict($"A file named '{path}' already exists. Choose another name or enable overwrite.");
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.
Related errors
- Cannot create directory because the path
- Cannot copy file ' ' because a file already exists in the…
- Cannot create file ' ' because it already exists.
- returned a null .
- Error retrieving file info for
AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13).
Data as JSON: /api/errors/b6f103800975b6e7.
Report an issue: GitHub.
Appendix: source
Thrown at src/OrchardCore/OrchardCore.FileStorage.FileSystem/FileSystemStore.cs:364
catch (FileStoreException)
{
throw;
}
catch (Exception ex)
{
throw new FileStoreException($"Cannot get file stream of the file '{fileStoreEntry.Path}'.", ex);
}
}
public async Task<string> CreateFileFromStreamAsync(string path, Stream inputStream, bool overwrite = false)
{
try
{
var physicalPath = GetPhysicalPath(path);
if (!overwrite && File.Exists(physicalPath))
{
throw new FileStoreException($"Cannot create file '{path}' because it already exists.");
}
if (Directory.Exists(physicalPath))
{
throw new FileStoreException($"Cannot create file '{path}' because it already exists as a directory.");
}
// Create directory path if it doesn't exist.
var physicalDirectoryPath = Path.GetDirectoryName(physicalPath);
Directory.CreateDirectory(physicalDirectoryPath);
var fileInfo = new FileInfo(physicalPath);
await using var outputStream = fileInfo.Create();
await inputStream.CopyToAsync(outputStream);
return path;
}
catch (FileStoreException)View on GitHub (pinned to 4306c0717f)