cefsharp/CefSharp · error · FileNotFoundException

Unable to create FileResourceHandler

Error message

Unable to create FileResourceHandler

What it means

Thrown by the FileResourceHandler constructor when filePath is non-empty but File.Exists returns false. CefStreamReader::CreateForFile would fail or produce an invalid stream for a missing file, so the constructor fails fast with FileNotFoundException (message 'Unable to create FileResourceHandler', fileName = filePath).

Source

Thrown at CefSharp/Internals/FileResourceHandler.cs:49

        /// Initializes a new instance of the <see cref="FileResourceHandler"/> class.
        /// </summary>
        /// <param name="mimeType">mimeType</param>
        /// <param name="filePath">filePath</param>
        public FileResourceHandler(string mimeType, string filePath)
        {
            if (string.IsNullOrEmpty(mimeType))
            {
                throw new ArgumentNullException("mimeType", "Please provide a valid mimeType");
            }

            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath", "Please provide a valid filePath");
            }

            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("Unable to create FileResourceHandler", filePath);
            }

            MimeType = mimeType;
            FilePath = filePath;
        }

        bool IResourceHandler.ProcessRequest(IRequest request, ICallback callback)
        {
            //Should never be called
            throw new NotImplementedException("This method should never be called");
        }

        void IResourceHandler.GetResponseHeaders(IResponse response, out long responseLength, out string redirectUrl)
        {
            //Should never be called
            throw new NotImplementedException("This method should never be called");
        }

View on GitHub (pinned to 16bc6e0711)

Solutions

  1. Use an absolute path: Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath).
  2. Verify the file exists and is readable before constructing; log the resolved absolute path to diagnose working-directory issues.
  3. Ensure the asset is copied to the output directory (CopyToOutputDirectory) in project settings.
  4. On case-sensitive file systems, confirm the path casing matches exactly.

Example fix

// before
var path = "assets/index.html";
return new FileResourceHandler("text/html", path);

// after
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "assets", "index.html");
if (!File.Exists(path)) return null;
return new FileResourceHandler("text/html", path);
Defensive patterns

Strategy: validation

Validate before calling

var absolute = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath);
if (!File.Exists(absolute))
{
    return null; // or log and serve a fallback
}
return new FileResourceHandler(mimeType, absolute);

Type guard

static bool FileExistsAndReadable(string path) => !string.IsNullOrEmpty(path) && File.Exists(path);

Prevention

When it happens

Trigger: Constructing new FileResourceHandler("text/html", path) where path points to a file that does not exist at construction time: wrong working directory, typo, file not yet written, deleted resource, or permission issue making the file invisible to File.Exists.

Common situations: Relative paths resolved against the wrong base directory (especially under IIS/desktop vs test runner); deployment that omitted the asset; file generated on demand but not yet present; case-sensitivity mismatch on Linux for Windows-authored paths; permissions hiding the file.

Related errors


AI-assisted analysis of cefsharp/CefSharp@16bc6e0711 (2026-08-13). Data as JSON: /api/errors/79f5304484b1cd3d. Report an issue: GitHub.