{"record":{"id":"d8270f34ef00a740","repo":"dotnet/wpf","slug":"sr-format-sr-readerversionerror-fileversion-readerversion","errorCode":null,"errorMessage":"SR.Format(SR.ReaderVersionError, _fileVersion.ReaderVersion, _codeVersion)","messagePattern":"SR\\.Format\\(SR\\.ReaderVersionError, _fileVersion\\.ReaderVersion, _codeVersion\\)","errorType":"exception","errorClass":"FileFormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/VersionedStreamOwner.cs","lineNumber":309,"sourceCode":"            CheckDisposed();    // central location\n\n            // only do this once\n            if (!_readOccurred)\n            {\n                // read\n                EnsureParsed();\n\n                // first usage?\n                if (throwIfEmpty || BaseStream.Length > 0)\n                {\n                    if (_fileVersion == null)\n                        throw new FileFormatException(SR.VersionStreamMissing);\n\n                    // compare versions\n                    // verify we can read this version\n                    if (!_fileVersion.IsReadableBy(_codeVersion.ReaderVersion))\n                    {\n                        throw new FileFormatException(\n                                        SR.Format(\n                                            SR.ReaderVersionError,\n                                            _fileVersion.ReaderVersion,\n                                            _codeVersion\n                                            )\n                                        );\n                    }\n                }\n                _readOccurred = true;\n            }\n        }\n\n        //------------------------------------------------------\n        //\n        //  Private Methods\n        //\n        //------------------------------------------------------\n        /// <summary>","sourceCodeStart":291,"sourceCodeEnd":327,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/VersionedStreamOwner.cs#L291-L327","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Upgrade the runtime/library so its reader version can read the file's format version","Copy or re-export the file with a compatible (older) writer version on a machine that can read it","Catch FileFormatException and present a clear 'file requires a newer version' message to the user instead of crashing","Keep framework versions uniform across machines that share compound files"],"exampleFix":"// before: direct read that fails on newer files\nint n = versionedStream.Read(buffer, 0, buffer.Length); // ReaderVersionError\n// after: catch and report the version mismatch\ntry\n{\n    int n = versionedStream.Read(buffer, 0, buffer.Length);\n}\ncatch (FileFormatException ex)\n{\n    throw new ApplicationException(\n        \"This file was written by a newer version and cannot be read here. Please upgrade.\", ex);\n}","handlingStrategy":"try-catch","validationCode":"// compare the file's recorded ReaderVersion with the current runtime's before reading\n// bool readable = RecordedFileVersion.IsReadableBy(CurrentCodeVersion.ReaderVersion); // do this in a pre-pass if the header is accessible","typeGuard":null,"tryCatchPattern":"try\n{\n    int n = versionedStream.Read(buffer, 0, buffer.Length);\n}\ncatch (FileFormatException ex) when (ex.Message.Contains(\"read\") || ex.Message.Contains(\"version\"))\n{\n    throw new ApplicationException(\"This file requires a newer application version. Please upgrade to open it.\", ex);\n}","preventionTips":["Standardize runtime versions across environments sharing files","When downgrading environments, re-export files into the older format first","Show actionable upgrade messaging when catching ReaderVersionError","Test file interchange between the oldest and newest supported runtime versions"],"tags":["versioning","compatibility","read","wpf"],"backgroundTag":"incompatible-source-type","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}