{"record":{"id":"5d7e3bd41b5e4854","repo":"dotnet/wpf","slug":"sr-seekfailed","errorCode":null,"errorMessage":"SR.SeekFailed","messagePattern":"SR\\.SeekFailed","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs","lineNumber":153,"sourceCode":"\n            set\n            {\n                CheckDisposedStatus();\n\n                if (!CanSeek)\n                {\n                    throw new NotSupportedException(SR.SetPositionNotSupported);\n                }\n                \n                long seekPos = 0;\n\n                _securitySuppressedIStream.Seek(value,\n                                                      NativeMethods.STREAM_SEEK_SET,\n                                                      out seekPos);\n\n                if (value != seekPos)\n                {\n                    throw new IOException(SR.SeekFailed);\n                }\n            }\n        }\n\n        #endregion Public Properties\n\n\n        //------------------------------------------------------\n        //\n        //  Public Methods\n        //\n        //------------------------------------------------------\n        #region Public Methods\n\n        public override void Flush()\n        {\n            // deliberate overidding noop\n            // do not want to do anything on a read-only stream","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs#L135-L171","documentation":"ByteStream wraps a COM IStream and, when setting the Position property, calls IStream.Seek(STREAM_SEEK_SET) and verifies the stream actually landed at the requested offset. If the resulting position (seekPos) differs from the requested value, it throws IOException(SR.SeekFailed). This indicates the underlying stream refused or failed the seek, so the position cannot be trusted.","triggerScenarios":"Setting the Position property (ByteStream.Position setter) to a value the wrapped IStream cannot seek to, e.g. a non-seekable COM stream, a stream shorter than the requested offset in some implementations, or a COM failure (STG_E_* error) that leaves seekPos != value.","commonSituations":"Opening XPS/ZIP packages over an IStream from a degraded source (partial download, removed device), or interop streams that report seek success but return a different position. Common when streaming package parts from network or removable media that disconnects mid-operation.","solutions":["Verify the underlying data source is complete and available (file fully downloaded, device connected) before opening the package.","Check that the IStream backing the ByteStream genuinely supports seeking (stat the stream / confirm it is not a purely sequential pipe).","Open the package from a local, seekable Stream (e.g. FileStream over a temp file) instead of a live network IStream.","Catch IOException and re-create/re-open the ByteStream rather than retrying the position set on a broken stream."],"exampleFix":"// before\nstream.Position = offset; // IOException SR.SeekFailed on flaky IStream\n// after\nif (!stream.CanSeek)\n    throw new InvalidOperationException(\"Backing stream is not seekable\");\ntry { stream.Position = offset; }\ncatch (IOException) { stream.Dispose(); stream = File.OpenRead(localCopyPath); stream.Position = offset; }","handlingStrategy":"try-catch","validationCode":"if (!stream.CanSeek) throw new InvalidOperationException(\"Underlying stream is not seekable\");","typeGuard":null,"tryCatchPattern":"try { stream.Position = offset; }\ncatch (IOException ex) when (ex.Message.Contains(\"Seek\")) { /* re-open stream from a local, seekable source */ }","preventionTips":["Open packages from fully materialized, local, seekable streams","Check CanSeek before random access","Validate data sources (downloads, removable media) are complete before reading"],"tags":["io","seek","istream","wpf-packaging"],"backgroundTag":"file-read-failed","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"}