dotnet/wpf · error · PathTooLongException

SR.PathTooLongExceptionWithFileName

Error message

SR.PathTooLongExceptionWithFileName

What it means

Thrown by ThrowWin32Exception when the Win32 error ERROR_FILENAME_EXCED_RANGE is returned while opening a font file for reading: the combined path (or a component of it) exceeds the MAX_PATH limit, so the font cache cannot open the file at the given fileName.

Solutions

  1. Shorten the font file path (move the font closer to the drive root or shorten folder names).
  2. Use a long-path aware configuration (longPathsEnabled registry key and longPathAware manifest) on .NET versions that support it.
  3. Use the \\?\ prefix for extended-length paths where the API supports it.
  4. For pack:// Uris, shorten the application/resource path.

Example fix

// before
var uri = new Uri(@"D:\Very\Long\Build\...\DeeplyNested\Output\Fonts\MyVeryLongFontFileName-Regular.ttf");
// after — ship fonts at a short stable location
var uri = new Uri(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Fonts", "MyFont.ttf"));
Defensive patterns

Strategy: validation

Validate before calling

if (fontUri.IsFile && fontUri.LocalPath.Length >= 260)
    throw new InvalidOperationException("Font path exceeds MAX_PATH; shorten it.");

Type guard

static bool FontPathIsShortEnough(Uri fontUri, int max = 260) =>
    !fontUri.IsFile || fontUri.LocalPath.Length < max;

Try / catch

try
{
    var gt = new GlyphTypeface(fontUri);
}
catch (PathTooLongException)
{
    // relocate font to a short path and retry
}

Prevention

When it happens

Trigger: Opening a font source whose fully qualified path exceeds the Win32 path length limit, producing error 206 which maps to PathTooLongException.

Common situations: Deeply nested build/checkout directories (node_modules-like trees), long temp paths, long network share names combined with a long font file name.

Related errors


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

Appendix: source

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

        internal static void ThrowWin32Exception(int errorCode, string fileName)
        {
            switch (errorCode)
            {
                case NativeMethods.ERROR_FILE_NOT_FOUND:
                    throw new FileNotFoundException(SR.Format(SR.FileNotFoundExceptionWithFileName, fileName), fileName);

                case NativeMethods.ERROR_PATH_NOT_FOUND:
                    throw new DirectoryNotFoundException(SR.Format(SR.DirectoryNotFoundExceptionWithFileName, fileName));

                case NativeMethods.ERROR_ACCESS_DENIED:
                    throw new UnauthorizedAccessException(SR.Format(SR.UnauthorizedAccessExceptionWithFileName, fileName));

                case NativeMethods.ERROR_FILENAME_EXCED_RANGE:
                    throw new PathTooLongException(SR.Format(SR.PathTooLongExceptionWithFileName, fileName));

                default:
                    throw new IOException(SR.Format(SR.IOExceptionWithFileName, fileName), NativeMethods.MakeHRFromErrorCode(errorCode));
            }
        }

View on GitHub (pinned to 81131a70a4)