{"record":{"id":"32226faf0e839ac5","repo":"dotnet/wpf","slug":"stream-does-not-support-seek","errorCode":null,"errorMessage":"Stream does not support Seek.","messagePattern":"Stream does not support Seek\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs","lineNumber":190,"sourceCode":"        }\n\n        /// <summary>\n        /// Sets the position within the current stream.\n        /// </summary>\n        /// <remarks>\n        /// ByteStream relies on underlying COM interface to\n        /// validate offsets.\n        /// </remarks>\n        /// <param name=\"offset\">Offset byte count</param>\n        /// <param name=\"origin\">Offset origin</param>\n        /// <returns>The new position within the current stream.</returns>\n        public override long Seek(long offset, SeekOrigin origin)\n        {\n            CheckDisposedStatus();\n\n            if (!CanSeek)\n            {\n                throw new NotSupportedException(SR.SeekNotSupported);\n            }\n\n            long seekPos = 0;\n            int translatedSeekOrigin = 0;\n\n            switch (origin)\n            {\n                case SeekOrigin.Begin:\n                    translatedSeekOrigin = NativeMethods.STREAM_SEEK_SET;\n                    if (0 > offset)\n                    {\n                        throw new ArgumentOutOfRangeException(nameof(offset),\n                                                              SR.SeekNegative);\n                    }\n                    break;\n\n                case SeekOrigin.Current:\n                    translatedSeekOrigin = NativeMethods.STREAM_SEEK_CUR;","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs#L172-L208","documentation":"ByteStream.Seek calls CheckDisposedStatus then verifies the CanSeek property before delegating to the wrapped IStream. If the stream cannot seek (CanSeek is false, e.g. the underlying IStream is null/unavailable), it throws NotSupportedException(SR.SeekNotSupported). The operation is simply not supported by this stream instance.","triggerScenarios":"Calling Seek(offset, origin) on a ByteStream whose CanSeek is false - typically when the underlying security-suppressed IStream was never supplied or became unavailable.","commonSituations":"Code written against a generic Stream abstraction that assumes Seek works, applied to a ByteStream constructed over a non-seekable IStream; random-access readers (ZipPackage, XPS document readers) consuming such a stream.","solutions":["Check stream.CanSeek before calling Seek and fall back to sequential reading when it is false.","Construct the ByteStream (or the package) over a seekable source such as a FileStream or MemoryStream.","Ensure the ByteStream is not disposed and its IStream was properly provided at construction."],"exampleFix":"// before\nstream.Seek(partOffset, SeekOrigin.Begin);\n// after\nif (stream.CanSeek) stream.Seek(partOffset, SeekOrigin.Begin);\nelse ReadSequentially(stream, partOffset);","handlingStrategy":"validation","validationCode":"if (!stream.CanSeek) throw new InvalidOperationException(\"Seek requires a seekable stream\");","typeGuard":"bool IsSeekable(Stream s) => s is ByteStream ? s.CanSeek : s.CanSeek;","tryCatchPattern":"try { stream.Seek(off, SeekOrigin.Begin); }\ncatch (NotSupportedException) { /* fall back to sequential read */ }","preventionTips":["Check Stream.CanSeek before any Seek call","Prefer FileStream/MemoryStream for random access","Never reuse disposed stream references"],"tags":["io","seek","not-supported"],"backgroundTag":"unsupported-operation","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"}