OrchardCMS/OrchardCore · error · FileStoreException

Cannot copy file because the destination path

Error message

Cannot copy file because the destination path '{dstPath}' already exists.

What it means

Pre-flight check in FileSystemStore.CopyFileAsync: a file or directory already exists at the physical destination path. The store disallows overwriting on copy, so the operation is rejected before File.Copy is invoked.

Solutions

  1. Delete the existing destination first or pick a unique destination name.
  2. Make the copy step idempotent by checking GetFileInfoAsync(dstPath) and skipping if present.
  3. Append a timestamp/GUID to generated destination names.

Example fix

// before
await store.CopyFileAsync(src, "media/photo.png"); // exists on re-run
// after
if (await store.GetFileInfoAsync("media/photo.png") is null)
    await store.CopyFileAsync(src, "media/photo.png");
Defensive patterns

Strategy: validation

Validate before calling

if (await store.GetFileInfoAsync(dstPath) is not null || await store.GetDirectoryInfoAsync(dstPath) is not null)
    return; // already copied — make the step idempotent by skipping

Try / catch

try { await store.CopyFileAsync(srcPath, dstPath); }
catch (FileStoreException ex) when (ex.Message.Contains("already exists"))
{
    // skip or generate a unique destination
}

Prevention

When it happens

Trigger: Calling CopyFileAsync(srcPath, dstPath) where dstPath already exists as a file or directory, e.g. re-running an import that copies into the same media folder.

Common situations: Re-running recipes/import steps without idempotency; copying with a destination name derived from the source name that already exists; concurrent uploads picking the same name.

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


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/750cbcec07f235e1. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore/OrchardCore.FileStorage.FileSystem/FileSystemStore.cs:290

        }
    }

    public Task CopyFileAsync(string srcPath, string dstPath)
    {
        try
        {
            var physicalSrcPath = GetPhysicalPath(srcPath);

            if (!File.Exists(physicalSrcPath))
            {
                throw new FileStoreException($"The file '{srcPath}' does not exist.");
            }

            var physicalDstPath = GetPhysicalPath(dstPath);

            if (File.Exists(physicalDstPath) || Directory.Exists(physicalDstPath))
            {
                throw new FileStoreException($"Cannot copy file because the destination path '{dstPath}' already exists.");
            }

            File.Copy(GetPhysicalPath(srcPath), GetPhysicalPath(dstPath));

            return Task.CompletedTask;
        }
        catch (FileStoreException)
        {
            throw;
        }
        catch (Exception ex)
        {
            throw new FileStoreException($"Cannot copy file '{srcPath}' to '{dstPath}'.", ex);
        }
    }

    public Task<Stream> GetFileStreamAsync(string path)
    {

View on GitHub (pinned to 4306c0717f)