dotnet/wpf · error · ArgumentException

SR.Verify_FileExists

Error message

SR.Verify_FileExists

What it means

Verify.FileExists(string filePath, string parameterName) first runs IsNeitherNullNorEmpty on the path, then checks File.Exists(filePath); if the file is not present it throws ArgumentException(SR.Verify_FileExists, parameterName) with the path in the message. The library uses it to validate path arguments to file-based APIs before attempting I/O.

Solutions

  1. Verify the exact path exists yourself with File.Exists(Path.GetFullPath(path)) and log it - GetFullPath exposes what directory the relative path resolves against.
  2. Fix the path in configuration/deployment so the file is present where the app runs (copy to output, include in installer).
  3. Switch to an absolute path to remove working-directory dependence.
  4. Create the file beforehand if it is supposed to exist, or use an API variant that tolerates a missing file.

Example fix

// before
loader.Load("resources\dictionary.xml"); // ArgumentException: file not found (cwd differs)
// after
var path = Path.Combine(AppContext.BaseDirectory, "resources", "dictionary.xml");
if (!File.Exists(path)) throw new FileNotFoundException("dictionary resource missing", path);
loader.Load(path);
Defensive patterns

Strategy: validation

Validate before calling

var fullPath = Path.GetFullPath(filePath);
if (string.IsNullOrEmpty(fullPath))
    throw new ArgumentException("filePath must be non-empty", nameof(filePath));
if (!File.Exists(fullPath))
    throw new FileNotFoundException("Required file is missing", fullPath);

Type guard

bool FileReady([NotNullWhen(true)] string? path) => !string.IsNullOrEmpty(path) && File.Exists(Path.GetFullPath(path));

Try / catch

try
{
    LibraryCall(filePath);
}
catch (ArgumentException ex) when (ex.Message.Contains("file"))
{
    logger.LogError("File '{Path}' was not found (cwd={Cwd})", filePath, Directory.GetCurrentDirectory());
    throw;
}

Prevention

When it happens

Trigger: Passing a null/empty path (fails earlier with the NeitherNullNorEmpty errors) or a path to a file that does not exist at call time (relative path resolved against the wrong current directory, typo'd filename, file deleted between check and use).

Common situations: Hard-coded relative paths that depend on the process working directory; config pointing at a resource file not deployed with the app; case-sensitivity differences when moving from Windows to other filesystems; missing content/copy-to-output settings so the file never lands next to the executable.

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/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/9d6f78b5313e0c86. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/Verify.cs:117

            else if (notExpected.Equals(actual))
            {
                throw new ArgumentException(SR.Format(SR.Verify_AreNotEqual, notExpected), parameterName);
            }
        }

        /// <summary>
        /// Verifies the specified file exists.  Throws an ArgumentException if it doesn't.
        /// </summary>
        /// <param name="filePath">The file path to check for existence.</param>
        /// <param name="parameterName">Name of the parameter to include in the ArgumentException.</param>
        /// <remarks>This method demands FileIOPermission(FileIOPermissionAccess.PathDiscovery)</remarks>
        public static void FileExists(string filePath, string parameterName)
        {
            Verify.IsNeitherNullNorEmpty(filePath, parameterName);

            if (!File.Exists(filePath))
            {
                throw new ArgumentException(SR.Format(SR.Verify_FileExists, filePath), parameterName);
            }
        }
    }
}

View on GitHub (pinned to 81131a70a4)