{"record":{"id":"4b48bfd594c1980e","repo":"dotnet/wpf","slug":"fileformatexception","errorCode":null,"errorMessage":"FileFormatException","messagePattern":"FileFormatException","errorType":"exception","errorClass":"FileFormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontCacheUtil.cs","lineNumber":837,"sourceCode":"                        NativeMethods.FILE_SHARE_READ,\n                        null,\n                        NativeMethods.OPEN_EXISTING,\n                        0,\n                        IntPtr.Zero\n                        ))\n                    {\n                        if (fileHandle.IsInvalid)\n                        {\n                            Util.ThrowWin32Exception(Marshal.GetLastWin32Error(), fileName);\n                        }\n\n                        UnsafeNativeMethods.LARGE_INTEGER fileSize = new UnsafeNativeMethods.LARGE_INTEGER();\n                        if (!UnsafeNativeMethods.GetFileSizeEx(fileHandle, ref fileSize))\n                            throw new IOException(SR.Format(SR.IOExceptionWithFileName, fileName));\n\n                        size = (long)fileSize.QuadPart;\n                        if (size == 0)\n                            throw new FileFormatException(new Uri(fileName));\n\n                        _mappingHandle = UnsafeNativeMethods.CreateFileMapping(\n                            fileHandle,\n                            sa,\n                            UnsafeNativeMethods.PAGE_READONLY,\n                            0,\n                            0,\n                            null);\n                    }\n\n                    if (_mappingHandle.IsInvalid)\n                        throw new IOException(SR.Format(SR.IOExceptionWithFileName, fileName));\n\n                    _viewHandle = UnsafeNativeMethods.MapViewOfFileEx(_mappingHandle, UnsafeNativeMethods.FILE_MAP_READ, 0, 0, IntPtr.Zero, IntPtr.Zero);\n                    if (_viewHandle.IsInvalid)\n                        throw new IOException(SR.Format(SR.IOExceptionWithFileName, fileName));\n\n                    Initialize((byte*)_viewHandle.Memory, size, size, FileAccess.Read);","sourceCodeStart":819,"sourceCodeEnd":855,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontCacheUtil.cs#L819-L855","documentation":"FileFormatException is thrown by WPF's font cache when a font file opened for memory-mapped reading has zero length. In CompositeFontFileInfo/ConnectedFontComponent the code maps the font file into memory; a 0-byte file cannot back a valid font view, so the loader fails fast with FileFormatException carrying the font file's Uri.","triggerScenarios":"A font file referenced by the process (e.g. via a composite font, Glyphs, or system font enumeration) exists but has size 0 on disk: GetFileSizeEx succeeds, size == (long)fileSize.QuadPart is 0, and 'throw new FileFormatException(new Uri(fileName))' fires at FontCacheUtil.cs:837.","commonSituations":"Corrupted font installation where a .ttf/.otf was created but its content write failed; a font inside an app package that was truncated during deployment; antivirus or disk-full interrupting a font copy; a placeholder file created by sync tools (OneDrive/Dropbox) that never hydrated.","solutions":["Check the reported font file with Get-Item <path> and confirm Length is 0; delete or replace the empty font file with a valid copy.","Reinstall the font (copy the .ttf/.otf again, or run the font installer) to restore its contents.","If the font is in an application package, rebuild/redeploy the package so the embedded font resource is not truncated.","If you cannot identify the font from the Uri in the exception, clear the WPF font cache (delete %WINDIR%/ServiceProfiles/LocalService/AppData/Local/FontCache contents after stopping the Font Cache service) and retry."],"exampleFix":"// before: font file exists but is empty (Length == 0)\nC:\\Windows\\Fonts\\myfont.ttf  -> 0 bytes  -> FileFormatException\n// after: replace with a valid copy of the font file\nCopy-Item \\\\server\\fonts\\myfont.ttf C:\\Windows\\Fonts\\myfont.ttf -Force\n(Get-Item C:\\Windows\\Fonts\\myfont.ttf).Length  # > 0","handlingStrategy":"validation","validationCode":"var fi = new FileInfo(fontPath);\nif (!fi.Exists || fi.Length == 0)\n    throw new InvalidOperationException($\"Font file missing or empty: {fontPath}\");","typeGuard":"static bool IsUsableFontFile(string path) =>\n    File.Exists(path) && new FileInfo(path).Length > 0;","tryCatchPattern":"try { /* load font */ }\ncatch (FileFormatException ex) when (ex.Uri is not null)\n{\n    Log.Warn(\"Empty/invalid font file {Uri}; excluding from font set\", ex.Uri);\n    DisableFont(ex.Uri);\n}","preventionTips":["Validate font file size > 0 before registering fonts","Ship checksums for embedded fonts and verify at startup","Avoid placing placeholder/sync-pending files in font directories","Reinstall fonts whose content was truncated by AV or disk-full"],"tags":["fonts","wpf","file-io","memory-mapping"],"backgroundTag":"file-read-failed","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}