dotnet/machinelearning · error · FileNotFoundException

FileNotFound

Error message

FileNotFound

What it means

TextFieldParser.ValidatePath throws FileNotFoundException("FileNotFound") when the path passed to the file-based constructor does not exist on disk. Unlike File.Open, this parser checks existence explicitly and reports the generic Strings.FileNotFound message.

Source

Thrown at src/Microsoft.Data.Analysis/TextFieldParser.cs:587

                throw new ArgumentNullException(nameof(stream));
            }
            if (!stream.CanRead)
            {
                throw new ArgumentException(Strings.StreamDoesntSupportReading);
            }
            if (defaultEncoding == null)
            {
                throw new ArgumentNullException(nameof(defaultEncoding));
            }
            _reader = new StreamReader(stream, defaultEncoding, detectEncoding);
            ReadToBuffer();
        }

        private string ValidatePath(string path)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(Strings.FileNotFound);
            }
            return path;
        }

        private bool IgnoreLine(string line)
        {
            if (line == null)
            {
                return false;
            }
            string trimmedLine = line.Trim();
            if (trimmedLine.Length == 0)
            {
                return true;
            }
            if (_commentTokens != null)
            {
                string[] commentTokens = _commentTokens;

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Check File.Exists(path) before constructing and print the full absolute path on failure
  2. Use Path.GetFullPath to see what the relative path resolves to and correct it
  3. Fix the path or ship/copy the file with your deployment
  4. Prefer passing a FileStream you open yourself for clearer error messages

Example fix

// before
var parser = new TextFieldParser("data/input.csv");
// after
string fullPath = Path.GetFullPath("data/input.csv");
if (!File.Exists(fullPath))
    throw new FileNotFoundException($"Input file missing: {fullPath}", fullPath);
var parser = new TextFieldParser(fullPath);
Defensive patterns

Strategy: validation

Validate before calling

if (!File.Exists(Path.GetFullPath(path))) throw new FileNotFoundException($"CSV input not found: {Path.GetFullPath(path)}", path);

Try / catch

try { parser = new TextFieldParser(path); } catch (FileNotFoundException ex) { Console.Error.WriteLine($"Input file missing: {path}"); throw; }

Prevention

When it happens

Trigger: Constructing TextFieldParser(string path) with a path that does not exist — typo, wrong working directory, relative path resolved against an unexpected base, or the file deleted between check and use.

Common situations: Using a relative path when the process's current directory differs from what the app assumes; case/typo errors in filenames; deployment packaging that omitted a data file; temp files already cleaned up.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/9a0340a1fc0a30d9. Report an issue: GitHub.