OrchardCMS/OrchardCore · error · InvalidOperationException
Cannot create a stream for a directory.
Error message
Cannot create a stream for a directory.
What it means
EmbeddedDirectoryInfo represents a directory entry in the embedded file provider; directories cannot be opened as streams. CreateReadStream always throws InvalidOperationException to enforce this contract, matching IFileInfo behavior for directories.
Solutions
- Check IFileInfo.IsDirectory before calling CreateReadStream and skip or return a 404/handle directories.
- Verify the embedded resource path points to an actual file; print available resources via the assembly manifest.
- If the path may be a folder, append the expected file name or resolve the specific resource.
- Wrap in try/catch InvalidOperationException only as a last resort; prefer the IsDirectory guard.
Example fix
// before
using var stream = fileInfo.CreateReadStream();
// after
if (fileInfo.IsDirectory || !fileInfo.Exists)
{
return Results.NotFound();
}
using var stream = fileInfo.CreateReadStream(); Defensive patterns
Strategy: type-guard
Validate before calling
if (fileInfo is { Exists: true, IsDirectory: false }) { /* safe to open stream */ } Type guard
bool IsReadableFile(IFileInfo f) => f.Exists && !f.IsDirectory && f.Length >= 0;
Try / catch
try { using var s = fileInfo.CreateReadStream(); }
catch (InvalidOperationException) { return Results.NotFound(); } Prevention
- Always check IFileInfo.IsDirectory before CreateReadStream
- Verify embedded resource names against the assembly manifest
- Never build streams from path segments that may resolve to folders
When it happens
Trigger: Calling CreateReadStream() on an IFileInfo returned by the embedded/manifest embedded file provider when the requested path resolves to a directory (e.g. path 'MyFolder' without extension that exists as a folder in an embedded assembly), rather than a file.
Common situations: Enumerating embedded resources and blindly calling CreateReadStream on every entry, building a file-serving endpoint where the URL maps to a folder, or a path typo dropping the file name so the provider resolves the directory.
Related errors
- Error retrieving file info for
- Error creating directory
- Error deleting file
- Cannot copy file ' ' to ' '.
- Cannot get file stream of the file
AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13).
Data as JSON: /api/errors/e0e994bef9512602.
Report an issue: GitHub.
Appendix: source
Thrown at src/OrchardCore/OrchardCore.Abstractions/Modules/FileProviders/EmbeddedDirectoryInfo.cs:54
/// <summary>
/// The time when the directory was last written to.
/// </summary>
public DateTimeOffset LastModified => DateTimeOffset.MinValue;
/// <summary>
/// Always true.
/// </summary>
public bool IsDirectory => true;
/// <summary>
/// Always throws an exception because read streams are not support on directories.
/// </summary>
/// <exception cref="InvalidOperationException">Always thrown.</exception>
/// <returns>Never returns.</returns>
public Stream CreateReadStream()
{
throw new InvalidOperationException("Cannot create a stream for a directory.");
}
}
View on GitHub (pinned to 4306c0717f)