{"record":{"id":"9d6f78b5313e0c86","repo":"dotnet/wpf","slug":"sr-verify-fileexists","errorCode":null,"errorMessage":"SR.Verify_FileExists","messagePattern":"SR\\.Verify_FileExists","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/Verify.cs","lineNumber":117,"sourceCode":"            else if (notExpected.Equals(actual))\n            {\n                throw new ArgumentException(SR.Format(SR.Verify_AreNotEqual, notExpected), parameterName);\n            }\n        }\n\n        /// <summary>\n        /// Verifies the specified file exists.  Throws an ArgumentException if it doesn't.\n        /// </summary>\n        /// <param name=\"filePath\">The file path to check for existence.</param>\n        /// <param name=\"parameterName\">Name of the parameter to include in the ArgumentException.</param>\n        /// <remarks>This method demands FileIOPermission(FileIOPermissionAccess.PathDiscovery)</remarks>\n        public static void FileExists(string filePath, string parameterName)\n        {\n            Verify.IsNeitherNullNorEmpty(filePath, parameterName);\n\n            if (!File.Exists(filePath))\n            {\n                throw new ArgumentException(SR.Format(SR.Verify_FileExists, filePath), parameterName);\n            }\n        }\n    }\n}\n\n","sourceCodeStart":99,"sourceCodeEnd":123,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/Verify.cs#L99-L123","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Verify the exact path exists yourself with File.Exists(Path.GetFullPath(path)) and log it - GetFullPath exposes what directory the relative path resolves against.","Fix the path in configuration/deployment so the file is present where the app runs (copy to output, include in installer).","Switch to an absolute path to remove working-directory dependence.","Create the file beforehand if it is supposed to exist, or use an API variant that tolerates a missing file."],"exampleFix":"// before\nloader.Load(\"resources\\dictionary.xml\"); // ArgumentException: file not found (cwd differs)\n// after\nvar path = Path.Combine(AppContext.BaseDirectory, \"resources\", \"dictionary.xml\");\nif (!File.Exists(path)) throw new FileNotFoundException(\"dictionary resource missing\", path);\nloader.Load(path);","handlingStrategy":"validation","validationCode":"var fullPath = Path.GetFullPath(filePath);\nif (string.IsNullOrEmpty(fullPath))\n    throw new ArgumentException(\"filePath must be non-empty\", nameof(filePath));\nif (!File.Exists(fullPath))\n    throw new FileNotFoundException(\"Required file is missing\", fullPath);","typeGuard":"bool FileReady([NotNullWhen(true)] string? path) => !string.IsNullOrEmpty(path) && File.Exists(Path.GetFullPath(path));","tryCatchPattern":"try\n{\n    LibraryCall(filePath);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"file\"))\n{\n    logger.LogError(\"File '{Path}' was not found (cwd={Cwd})\", filePath, Directory.GetCurrentDirectory());\n    throw;\n}","preventionTips":["Use absolute paths built from AppContext.BaseDirectory instead of the working directory","Set Copy to Output Directory / include resource files in deployment for data files","Check File.Exists and log the resolved full path when diagnosing","Don't assume the file still exists after checking - handle FileNotFoundException too if it can race"],"tags":["file-system","file-not-found","argument-validation","paths"],"backgroundTag":"file-not-found","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}