restsharp/RestSharp · error · FileNotFoundException

File not found

Error message

File not found

What it means

Thrown by FileParameter.FromFile when the specified fullPath does not exist on disk (File.Exists returns false). Ensures the file is present before attempting to open a read stream for upload.

Source

Thrown at src/RestSharp/Parameters/FileParameter.cs:103

    /// <param name="contentType">Optional: parameter content type, default is "application/g-zip"</param>
    /// <param name="options">File parameter options</param>
    /// <returns>The <see cref="FileParameter" /> using the default content type.</returns>
    public static FileParameter Create(
        string                name,
        Func<Stream>          getFile,
        string                fileName,
        ContentType?          contentType = null,
        FileParameterOptions? options     = null
    )
        => new(name, fileName, getFile, contentType, options ?? new FileParameterOptions());

    public static FileParameter FromFile(
        string                fullPath,
        string?               name        = null,
        ContentType?          contentType = null,
        FileParameterOptions? options     = null
    ) {
        if (!File.Exists(Ensure.NotEmptyString(fullPath, nameof(fullPath)))) throw new FileNotFoundException("File not found", fullPath);

        var fileName      = Path.GetFileName(fullPath);
        var parameterName = name ?? fileName;

        return new(parameterName, fileName, GetFile, contentType, options ?? new FileParameterOptions());

        Stream GetFile() => File.OpenRead(fullPath);
    }
}

[PublicAPI]
public class FileParameterOptions {
    public bool DisableFilenameStar     { get; set; } = true;
    public bool DisableFilenameEncoding { get; set; }
}

View on GitHub (pinned to 6a50821692)

Solutions

  1. Verify the file exists (File.Exists) and the path is absolute before calling FromFile.
  2. Use AppContext.BaseDirectory or Path.Combine to anchor relative paths.
  3. Ensure the file is produced (built/copied) before the upload step runs.

Example fix

// before
request.AddFile("file", config.AttachmentPath);

// after
var fullPath = Path.Combine(AppContext.BaseDirectory, config.AttachmentPath);
if (!File.Exists(fullPath))
    throw new FileNotFoundException($"Attachment not found: {fullPath}");
request.AddFile("file", fullPath);
Defensive patterns

Strategy: validation

Validate before calling

if (!File.Exists(path)) throw new FileNotFoundException("File not found", path);

Try / catch

try { request.AddFile("file", path); } catch (FileNotFoundException) { /* handle missing file gracefully */ }

Prevention

When it happens

Trigger: Calling FileParameter.FromFile(path) or request.AddFile(path) where path points to a non-existent, moved, or inaccessible file.

Common situations: Relative path resolved against an unexpected working directory; file deleted between check and upload; path from config pointing to a build artifact not yet produced; permissions/typo in the path.

Related errors


AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13). Data as JSON: /api/errors/fec21f31f435087b. Report an issue: GitHub.