OrchardCMS/OrchardCore · error · FileStoreException

Cannot create directory because the path

Error message

Cannot create directory because the path '{path}' already exists and is a file.

What it means

FileSystemStore wraps the physical file system and throws FileStoreException for store-level failures. This error is thrown when TryCreateDirectoryAsync is asked to create a directory at a virtual path that maps to an existing physical FILE. The store refuses to overwrite a file with a directory rather than corrupting the store.

Solutions

  1. Check with await store.GetFileInfoAsync(path) (or File.Exists on the physical path) before creating the directory and delete/rename the conflicting file first.
  2. Use a different path for the directory.
  3. Ensure callers never pass file paths to directory-creation APIs.

Example fix

// before
await store.TryCreateDirectoryAsync("uploads/logo"); // 'logo' is an existing file
// after
var existing = await store.GetFileInfoAsync("uploads/logo");
if (existing is not null) { await store.TryDeleteFileAsync("uploads/logo"); }
await store.TryCreateDirectoryAsync("uploads/logo");
Defensive patterns

Strategy: validation

Validate before calling

var dirInfo = await store.GetDirectoryInfoAsync(path);
var fileInfo = await store.GetFileInfoAsync(path);
if (fileInfo is not null) throw new InvalidOperationException($"'{path}' is a file, not a directory");
if (dirInfo is not null) return; // already exists

Type guard

static bool IsUsableDirectoryPath(IFileStoreEntry entry) => entry is not null && entry.IsDirectory;

Try / catch

try { await store.TryCreateDirectoryAsync(path); }
catch (FileStoreException ex) when (ex.Message.Contains("already exists and is a file"))
{
    // resolve the file/dir conflict before retrying
}

Prevention

When it happens

Trigger: Calling TryCreateDirectoryAsync(path) where GetPhysicalPath(path) points to an existing file, e.g. a media file was previously stored at 'folder' (no extension) and code later calls TryCreateDirectoryAsync("folder").

Common situations: Path casing/extension mistakes in media library code; importing content that assumes 'folder' is a directory when a file of that name exists; race between file upload and directory creation on the same path.

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

Appendix: source

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

                });

            return results.ToAsyncEnumerable();
        }
        catch (Exception ex)
        {
            throw new FileStoreException($"Cannot get directories with path '{path}'.", ex);
        }
    }

    public Task<bool> TryCreateDirectoryAsync(string path)
    {
        try
        {
            var physicalPath = GetPhysicalPath(path);

            if (File.Exists(physicalPath))
            {
                throw new FileStoreException($"Cannot create directory because the path '{path}' already exists and is a file.");
            }

            if (Directory.Exists(physicalPath))
            {
                return Task.FromResult(false);
            }

            Directory.CreateDirectory(physicalPath);

            return Task.FromResult(true);
        }
        catch (FileStoreException)
        {
            throw;
        }
        catch (Exception ex)
        {
            throw new FileStoreException($"Cannot create directory '{path}'.", ex);

View on GitHub (pinned to 4306c0717f)