dotnet/wpf · error · FileFormatException

SR.Format(SR.ReaderVersionError…

Error message

SR.Format(SR.ReaderVersionError, _fileVersion.ReaderVersion, _codeVersion)

What it means

FileFormatException (SR.ReaderVersionError) thrown by VersionedStreamOwner.ReadAttempt when the version header found in the stream (_fileVersion.ReaderVersion) is not readable by the running code's reader version (_codeVersion.ReaderVersion). This blocks reading files whose persisted format is newer than the code can understand, preventing silent data corruption.

Solutions

  1. Upgrade the runtime/library so its reader version can read the file's format version
  2. Copy or re-export the file with a compatible (older) writer version on a machine that can read it
  3. Catch FileFormatException and present a clear 'file requires a newer version' message to the user instead of crashing
  4. Keep framework versions uniform across machines that share compound files

Example fix

// before: direct read that fails on newer files
int n = versionedStream.Read(buffer, 0, buffer.Length); // ReaderVersionError
// after: catch and report the version mismatch
try
{
    int n = versionedStream.Read(buffer, 0, buffer.Length);
}
catch (FileFormatException ex)
{
    throw new ApplicationException(
        "This file was written by a newer version and cannot be read here. Please upgrade.", ex);
}
Defensive patterns

Strategy: try-catch

Validate before calling

// compare the file's recorded ReaderVersion with the current runtime's before reading
// bool readable = RecordedFileVersion.IsReadableBy(CurrentCodeVersion.ReaderVersion); // do this in a pre-pass if the header is accessible

Try / catch

try
{
    int n = versionedStream.Read(buffer, 0, buffer.Length);
}
catch (FileFormatException ex) when (ex.Message.Contains("read") || ex.Message.Contains("version"))
{
    throw new ApplicationException("This file requires a newer application version. Please upgrade to open it.", ex);
}

Prevention

When it happens

Trigger: Calling Read, ReadByte, or a nested ReadAttempt on a stream whose FormatVersion records a reader version greater than what the current runtime supports — typically a file written by a newer framework version being read by an older one.

Common situations: Downgrade scenarios: files produced on a machine with a newer .NET/WPF opened on an older runtime; side-by-side runtime installs; artifacts shared across environments with mismatched framework versions.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

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

            CheckDisposed();    // central location

            // only do this once
            if (!_readOccurred)
            {
                // read
                EnsureParsed();

                // first usage?
                if (throwIfEmpty || BaseStream.Length > 0)
                {
                    if (_fileVersion == null)
                        throw new FileFormatException(SR.VersionStreamMissing);

                    // compare versions
                    // verify we can read this version
                    if (!_fileVersion.IsReadableBy(_codeVersion.ReaderVersion))
                    {
                        throw new FileFormatException(
                                        SR.Format(
                                            SR.ReaderVersionError,
                                            _fileVersion.ReaderVersion,
                                            _codeVersion
                                            )
                                        );
                    }
                }
                _readOccurred = true;
            }
        }

        //------------------------------------------------------
        //
        //  Private Methods
        //
        //------------------------------------------------------
        /// <summary>

View on GitHub (pinned to 81131a70a4)