dotnet/wpf · error · FileFormatException

String format is not valid.

Error message

String format is not valid.

What it means

Inside ReadByteLengthPrefixedDWordPaddedUnicodeString, the library verifies the underlying stream is long enough to contain the declared character data (reader.BaseStream.Length < bytesRead/2). When the stream is too short the declared string length exceeds available bytes, indicating a corrupt or truncated record; the desktop build throws FileFormatException(SR.InvalidStringFormat). This is a length-consistency check, not a formatting problem in user input.

Solutions

  1. Re-acquire the file and verify its size/checksum; the data is truncated or corrupt.
  2. Ensure the reader is positioned at the correct record start before Load (reset Position or re-open the stream).
  3. Confirm the file was transferred in binary mode and not altered by encoding conversion.
  4. Catch FileFormatException around the Load call and report the document as unreadable rather than retrying the same bytes.

Example fix

// before
var reference = CompoundFileReference.Load(reader); // throws if stream too short
// after
if (reader.BaseStream.Length - reader.BaseStream.Position < 4)
    throw new InvalidDataException("Stream too short to contain a compound-file reference.");
var reference = CompoundFileReference.Load(reader);
Defensive patterns

Strategy: try-catch

Validate before calling

if (reader.BaseStream.Length - reader.BaseStream.Position < 4) throw new InvalidDataException("Stream too short for length-prefixed string.");

Type guard

static bool HasEnoughBytes(Stream s, long needed) => s != null && s.CanSeek && (s.Length - s.Position) >= needed;

Try / catch

try { var r = CompoundFileReference.Load(reader); }
catch (FileFormatException ex) { throw new InvalidDataException("Compound file data is truncated or corrupt.", ex); }

Prevention

When it happens

Trigger: Calling CompoundFileReference.Load / CompoundFileStorageReference.Load on a stream where a length-prefixed string header claims more bytes than remain in the stream.

Common situations: Truncated downloads or copies, binary data corrupted by text-mode transfer, wrong stream position (reading from the middle of a file), or non-compound-file data passed to a compound-file reader.

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/27d2807713360a81. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/ContainerUtilities.cs:191

        {
            checked
            {
                bytesRead = 0;

                CheckAgainstNull(reader, "reader");

                bytesRead = reader.ReadInt32();   // Length of the string in bytes

                String inString = null;

                if (bytesRead > 0)
                {
                    try
                    {
                        if (reader.BaseStream.Length < bytesRead / 2)
                        {
#if !PBTCOMPILER
                            throw new FileFormatException(SR.InvalidStringFormat);
#else
                        throw new SerializationException(SR.InvalidStringFormat);
#endif
                        }
                    }
                    catch (NotSupportedException)
                    {
                        // if the stream does not support the Length operator, it will throw a NotSupportedException.
                    }

                    inString = new String(reader.ReadChars(bytesRead / 2));

                    // Make sure the length of string read matches the length specified
                    if (inString.Length != (bytesRead / 2))
                    {
#if !PBTCOMPILER
                        throw new FileFormatException(SR.InvalidStringFormat);
#else

View on GitHub (pinned to 81131a70a4)