dotnet/wpf · error · FileFormatException

FileFormatException

Error message

FileFormatException

What it means

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.

Solutions

  1. 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.
  2. Reinstall the font (copy the .ttf/.otf again, or run the font installer) to restore its contents.
  3. If the font is in an application package, rebuild/redeploy the package so the embedded font resource is not truncated.
  4. 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.

Example fix

// before: font file exists but is empty (Length == 0)
C:\Windows\Fonts\myfont.ttf  -> 0 bytes  -> FileFormatException
// after: replace with a valid copy of the font file
Copy-Item \\server\fonts\myfont.ttf C:\Windows\Fonts\myfont.ttf -Force
(Get-Item C:\Windows\Fonts\myfont.ttf).Length  # > 0
Defensive patterns

Strategy: validation

Validate before calling

var fi = new FileInfo(fontPath);
if (!fi.Exists || fi.Length == 0)
    throw new InvalidOperationException($"Font file missing or empty: {fontPath}");

Type guard

static bool IsUsableFontFile(string path) =>
    File.Exists(path) && new FileInfo(path).Length > 0;

Try / catch

try { /* load font */ }
catch (FileFormatException ex) when (ex.Uri is not null)
{
    Log.Warn("Empty/invalid font file {Uri}; excluding from font set", ex.Uri);
    DisableFont(ex.Uri);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/4b48bfd594c1980e. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontCacheUtil.cs:837

                        NativeMethods.FILE_SHARE_READ,
                        null,
                        NativeMethods.OPEN_EXISTING,
                        0,
                        IntPtr.Zero
                        ))
                    {
                        if (fileHandle.IsInvalid)
                        {
                            Util.ThrowWin32Exception(Marshal.GetLastWin32Error(), fileName);
                        }

                        UnsafeNativeMethods.LARGE_INTEGER fileSize = new UnsafeNativeMethods.LARGE_INTEGER();
                        if (!UnsafeNativeMethods.GetFileSizeEx(fileHandle, ref fileSize))
                            throw new IOException(SR.Format(SR.IOExceptionWithFileName, fileName));

                        size = (long)fileSize.QuadPart;
                        if (size == 0)
                            throw new FileFormatException(new Uri(fileName));

                        _mappingHandle = UnsafeNativeMethods.CreateFileMapping(
                            fileHandle,
                            sa,
                            UnsafeNativeMethods.PAGE_READONLY,
                            0,
                            0,
                            null);
                    }

                    if (_mappingHandle.IsInvalid)
                        throw new IOException(SR.Format(SR.IOExceptionWithFileName, fileName));

                    _viewHandle = UnsafeNativeMethods.MapViewOfFileEx(_mappingHandle, UnsafeNativeMethods.FILE_MAP_READ, 0, 0, IntPtr.Zero, IntPtr.Zero);
                    if (_viewHandle.IsInvalid)
                        throw new IOException(SR.Format(SR.IOExceptionWithFileName, fileName));

                    Initialize((byte*)_viewHandle.Memory, size, size, FileAccess.Read);

View on GitHub (pinned to 81131a70a4)