OrchardCMS/OrchardCore · error · FileStoreException

Cannot create directory

Error message

Cannot create directory '{path}'.

What it means

Generic catch-all in TryCreateDirectoryAsync: any unexpected exception from Directory.CreateDirectory (IO errors, unauthorized access, invalid path characters, disk full) is rethrown as FileStoreException with this message and the original exception as InnerException. It is not thrown for pre-existing directories (those return false).

Solutions

  1. Inspect the InnerException of the FileStoreException for the real cause.
  2. Grant write permission on the storage root to the application identity.
  3. Sanitize/validate the path components before calling TryCreateDirectoryAsync.

Example fix

// before
await store.TryCreateDirectoryAsync(userInput);
// after
var safe = string.Concat(userInput.Split(Path.GetInvalidFileNameChars()));
await store.TryCreateDirectoryAsync(safe);
Defensive patterns

Strategy: try-catch

Validate before calling

if (path.Split('/', StringSplitOptions.RemoveEmptyEntries).Any(p => p.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)) throw new ArgumentException($"Invalid path segment in '{path}'");

Type guard

static bool IsValidStorePath(string path) => !string.IsNullOrWhiteSpace(path) && path.IndexOfAny(Path.GetInvalidPathChars()) < 0;

Try / catch

try { await store.TryCreateDirectoryAsync(path); }
catch (FileStoreException ex)
{
    _logger.LogError(ex.InnerException, "Directory creation failed for {Path}", path);
    throw;
}

Prevention

When it happens

Trigger: TryCreateDirectoryAsync(path) when the underlying Directory.CreateDirectory throws: access denied, read-only volume, illegal characters in the mapped physical path, path too long, or disk full.

Common situations: App pool identity lacks write permission to App_Data/media; running on a read-only container filesystem; invalid characters in tenant-generated folder names; MAX_PATH issues on Windows.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


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

Appendix: source

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

                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);
        }
    }

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

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

            File.Delete(physicalPath);

            return Task.FromResult(true);
        }

View on GitHub (pinned to 4306c0717f)