{"record":{"id":"ac6fc16a672c66c8","repo":"dotnet/wpf","slug":"sr-directorynotfoundexceptionwithfilename","errorCode":null,"errorMessage":"SR.DirectoryNotFoundExceptionWithFileName","messagePattern":"SR\\.DirectoryNotFoundExceptionWithFileName","errorType":"exception","errorClass":"DirectoryNotFoundException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontCacheUtil.cs","lineNumber":751,"sourceCode":"            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;\n            if (fontSource.IsFile)\n            {\n                fileName = fontSource.Uri.LocalPath;","sourceCodeStart":733,"sourceCodeEnd":769,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontCacheUtil.cs#L733-L769","documentation":"Util.ThrowWin32Exception maps Win32 error codes from font-file operations to typed exceptions; ERROR_PATH_NOT_FOUND yields DirectoryNotFoundException with SR.DirectoryNotFoundExceptionWithFileName, meaning the directory portion of the font path does not exist.","triggerScenarios":"Opening a font source whose parent directory is missing (Win32 error 3), routed through Util.ThrowWin32Exception from font file open/mapping code.","commonSituations":"Typo in the directory part of a font:// or file:// Uri; deployment layout changed so the fonts folder is absent; drive letter or share renamed.","solutions":["Verify the directory portion of the font path exists (Directory.Exists) before loading.","Correct the font Uri path.","Fix deployment/packaging so the fonts directory ships with the app.","If using a UNC path, verify the share is reachable."],"exampleFix":"// before\nvar gt = new GlyphTypeface(new Uri(@\"C:\\Fonts\\Old\\App.ttf\")); // Fonts\\Old missing\n// after\nstring path = @\"C:\\Fonts\\New\\App.ttf\";\nif (!Directory.Exists(Path.GetDirectoryName(path))) throw new InvalidOperationException(\"Font directory missing\");\nvar gt = new GlyphTypeface(new Uri(path));","handlingStrategy":"validation","validationCode":"string dir = Path.GetDirectoryName(fontUri.LocalPath);\nif (fontUri.IsFile && !Directory.Exists(dir))\n    throw new InvalidOperationException($\"Font directory missing: {dir}\");","typeGuard":"static bool FontDirectoryExists(Uri fontUri) =>\n    fontUri.IsFile && Directory.Exists(Path.GetDirectoryName(fontUri.LocalPath));","tryCatchPattern":"try\n{\n    var gt = new GlyphTypeface(fontUri);\n}\ncatch (DirectoryNotFoundException ex)\n{\n    Log($\"Font directory missing: {ex.Message}\");\n    // correct deployment or fall back\n}","preventionTips":["Validate the parent directory of font paths at startup.","Keep font assets under the application's own folders.","Detect moved/renamed shares with a health check before font loading."],"tags":["directory-not-found","font","io","wpf"],"backgroundTag":"directory-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"}