OrchardCMS/OrchardCore · error · FileStoreException
Cannot move file because the new path
Error message
Cannot move file because the new path '{newPath}' already exists. What it means
MoveFileAsync refuses to overwrite: if a file OR directory already exists at newPath it throws FileStoreException. File.Move would otherwise throw anyway; the store pre-checks to give a clear message and to prevent clobbering directories.
Solutions
- Delete or rename the existing destination first (TryDeleteFileAsync / TryDeleteDirectoryAsync).
- Generate a unique destination name (append a counter or GUID).
- Check GetFileInfoAsync/GetDirectoryInfoAsync on newPath before moving.
Example fix
// before
await store.MoveFileAsync("a.txt", "b.txt"); // b.txt exists
// after
if (await store.GetFileInfoAsync("b.txt") is not null)
await store.TryDeleteFileAsync("b.txt");
await store.MoveFileAsync("a.txt", "b.txt"); Defensive patterns
Strategy: validation
Validate before calling
if (await store.GetFileInfoAsync(newPath) is not null || await store.GetDirectoryInfoAsync(newPath) is not null)
newPath = GenerateUniqueName(newPath); // append counter/GUID before moving Try / catch
try { await store.MoveFileAsync(oldPath, newPath); }
catch (FileStoreException ex) when (ex.Message.Contains("already exists"))
{
// pick a different destination or delete the existing one
} Prevention
- Generate unique destination names (GUID suffix) for user-driven renames.
- Pre-check the destination with GetFileInfoAsync/GetDirectoryInfoAsync.
- Never move onto a path used as a directory.
When it happens
Trigger: Calling MoveFileAsync(oldPath, newPath) where a file or folder already occupies newPath, e.g. renaming 'a.txt' to 'b.txt' when 'b.txt' exists, or to a path that is an existing directory.
Common situations: Media library rename colliding with an existing asset; two users renaming concurrently; generating the target name from user input without uniqueness checks.
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 copy file because the destination path
- Cannot create directory because the path
- Cannot create directory
- Cannot move file ' ' because it does not exist.
- The file ' ' does not exist.
AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13).
Data as JSON: /api/errors/47b5eb2cec04dc8c.
Report an issue: GitHub.
Appendix: source
Thrown at src/OrchardCore/OrchardCore.FileStorage.FileSystem/FileSystemStore.cs:258
}
}
public Task MoveFileAsync(string oldPath, string newPath)
{
try
{
var physicalOldPath = GetPhysicalPath(oldPath);
if (!File.Exists(physicalOldPath))
{
throw new FileStoreException($"Cannot move file '{oldPath}' because it does not exist.");
}
var physicalNewPath = GetPhysicalPath(newPath);
if (File.Exists(physicalNewPath) || Directory.Exists(physicalNewPath))
{
throw new FileStoreException($"Cannot move file because the new path '{newPath}' already exists.");
}
File.Move(physicalOldPath, physicalNewPath);
return Task.CompletedTask;
}
catch (FileStoreException)
{
throw;
}
catch (Exception ex)
{
throw new FileStoreException($"Cannot move file '{oldPath}' to '{newPath}'.", ex);
}
}
public Task CopyFileAsync(string srcPath, string dstPath)
{View on GitHub (pinned to 4306c0717f)