OrchardCMS/OrchardCore · error · FileStoreException

Cannot create file ' ' because it already exists as a…

Error message

Cannot create file '{path}' because it already exists as a directory.

What it means

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.

Solutions

  1. 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.
  2. Correct the calling code so the path points to a file name, not a folder (append the intended filename to the folder path).
  3. If the directory is a leftover artifact, delete it via IFileStore.TryDeleteDirectoryAsync and retry the create.
  4. Wrap the call in try/catch on FileStoreException to surface a clear message to the user instead of an unhandled exception.

Example fix

// before
await fileStore.CreateFileFromStreamAsync("media/uploads", stream);
// after
var target = "media/uploads/report.pdf";
if (await fileStore.GetDirectoryInfoAsync(target) is not null)
{
    throw new InvalidOperationException($"'{target}' is an existing directory; choose a file path.");
}
await fileStore.CreateFileFromStreamAsync(target, stream);
Defensive patterns

Strategy: validation

Validate before calling

if (await fileStore.GetDirectoryInfoAsync(path) is not null)
{
    throw new InvalidOperationException($"'{path}' is an existing directory; cannot create a file there.");
}

Try / catch

try
{
    await fileStore.CreateFileFromStreamAsync(path, stream);
}
catch (FileStoreException ex) when (ex.Message.Contains("as a directory"))
{
    logger.LogWarning("Target '{Path}' is a directory; adjust the path.", path);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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).

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/3cf23e6965461467. Report an issue: GitHub.

Appendix: source

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

        {
            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)
        {
            throw;
        }
        catch (Exception ex)
        {

View on GitHub (pinned to 4306c0717f)