{"record":{"id":"0f053fde3858c4b2","repo":"dotnet/wpf","slug":"sr-filenotfoundexceptionwithfilename","errorCode":null,"errorMessage":"SR.FileNotFoundExceptionWithFileName","messagePattern":"SR\\.FileNotFoundExceptionWithFileName","errorType":"exception","errorClass":"FileNotFoundException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontCacheUtil.cs","lineNumber":748,"sourceCode":"        {\n            char ca = Char.ToUpperInvariant(a);\n            char cb = Char.ToUpperInvariant(b);\n            return ca - cb;\n        }\n\n        /// <summary>\n        /// This function performs job similar to CLR's internal __Error.WinIOError function:\n        /// it maps win32 errors from file I/O to CLR exceptions and includes string where possible.\n        /// However, we're interested only in errors when opening a file for reading.\n        /// </summary>\n        /// <param name=\"errorCode\">Win32 error code.</param>\n        /// <param name=\"fileName\">File name string.</param>\n        internal static void ThrowWin32Exception(int errorCode, string fileName)\n        {\n            switch (errorCode)\n            {\n                case NativeMethods.ERROR_FILE_NOT_FOUND:\n                    throw new FileNotFoundException(SR.Format(SR.FileNotFoundExceptionWithFileName, fileName), fileName);\n\n                case NativeMethods.ERROR_PATH_NOT_FOUND:\n                    throw new DirectoryNotFoundException(SR.Format(SR.DirectoryNotFoundExceptionWithFileName, fileName));\n\n                case NativeMethods.ERROR_ACCESS_DENIED:\n                    throw new UnauthorizedAccessException(SR.Format(SR.UnauthorizedAccessExceptionWithFileName, fileName));\n\n                case NativeMethods.ERROR_FILENAME_EXCED_RANGE:\n                    throw new PathTooLongException(SR.Format(SR.PathTooLongExceptionWithFileName, fileName));\n\n                default:\n                    throw new IOException(SR.Format(SR.IOExceptionWithFileName, fileName), NativeMethods.MakeHRFromErrorCode(errorCode));\n            }\n        }\n\n        internal static Exception ConvertInPageException(FontSource fontSource, SEHException e)\n        {\n            string fileName;","sourceCodeStart":730,"sourceCodeEnd":766,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontCacheUtil.cs#L730-L766","documentation":"Util.ThrowWin32Exception maps a Win32 error code from a font-file operation to a strongly typed .NET exception. When errorCode is ERROR_FILE_NOT_FOUND it throws FileNotFoundException with the file name embedded via SR.FileNotFoundExceptionWithFileName.","triggerScenarios":"A font file access API (e.g. FontSource / GlyphTypeface loading a file-based font Uri) failing with Win32 error 2 (ERROR_FILE_NOT_FOUND), routed through Util.ThrowWin32Exception.","commonSituations":"Font file deleted or moved after the Uri was created; wrong path in a file:// font Uri; font embedded resource not copied to output; network share font unavailable.","solutions":["Verify the font file exists at the Uri path (File.Exists) before creating the GlyphTypeface/FontSource.","Correct the font Uri/path, including pack:// syntax for embedded resources.","Ensure embedded font resources have Build Action = Resource and are actually compiled into the assembly.","Check network share availability/permissions if the font resides on a UNC path."],"exampleFix":"// before\nvar gt = new GlyphTypeface(new Uri(@\"C:\\Fonts\\Missing.ttf\"));\n// after\nstring path = @\"C:\\Fonts\\Missing.ttf\";\nif (!File.Exists(path)) throw new InvalidOperationException($\"Font not found: {path}\");\nvar gt = new GlyphTypeface(new Uri(path));","handlingStrategy":"validation","validationCode":"string path = fontUri.IsFile ? fontUri.LocalPath : null;\nif (path != null && !File.Exists(path))\n    throw new InvalidOperationException($\"Font file not found: {path}\");","typeGuard":"static bool FontFileExists(Uri fontUri) =>\n    fontUri.IsFile && File.Exists(fontUri.LocalPath);","tryCatchPattern":"try\n{\n    var gt = new GlyphTypeface(fontUri);\n}\ncatch (FileNotFoundException ex)\n{\n    Log($\"Font file missing: {ex.FileName}\");\n    // fall back to a bundled font\n}","preventionTips":["Check File.Exists on file Uris before loading fonts.","Ship fonts with the app (Build Action = Resource) rather than depending on absolute paths.","Verify deployment includes the fonts directory."],"tags":["file-not-found","font","io","wpf"],"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"}