{"record":{"id":"441aaf9f910f6733","repo":"dotnet/wpf","slug":"sr-setpositionnotsupported","errorCode":null,"errorMessage":"SR.SetPositionNotSupported","messagePattern":"SR\\.SetPositionNotSupported","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs","lineNumber":142,"sourceCode":"            {\n                CheckDisposedStatus();\n                \n                long seekPos = 0;\n\n                _securitySuppressedIStream.Seek(0,\n                                                      NativeMethods.STREAM_SEEK_CUR,\n                                                      out seekPos);\n\n                return seekPos;\n            }\n\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","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs#L124-L160","documentation":"ByteStream wraps a COM IStream and implements a Stream-like Position setter. When the underlying stream is not seekable, setting Position throws NotSupportedException (SR.SetPositionNotSupported); writes go through CheckDisposedStatus first, so a disposed stream would already have thrown before this check.","triggerScenarios":"Assigning ByteStream.Position (or calling Stream APIs that set it, like Seek with an absolute offset) on a ByteStream whose CanSeek is false — typically a forward-only pipe/device IStream that does not support STREAM_SEEK_SET.","commonSituations":"Treating pack-URI streams from non-seekable COM sources like files (network pipes, printers, in-progress downloads) as random-access; buffering code that assumes every Stream supports seeking.","solutions":["Check ByteStream.CanSeek before setting Position and skip the seek when false.","Copy the stream into a MemoryStream to obtain a seekable copy before random access.","Read forward sequentially instead of repositioning.","Ensure the source COM object exposed to ByteStream genuinely supports IStream::Seek."],"exampleFix":"// before\nstream.Position = 0;\n// after\nif (stream.CanSeek) { stream.Position = 0; } else { var copy = new MemoryStream(); stream.CopyTo(copy); copy.Position = 0; }","handlingStrategy":"type-guard","validationCode":"bool seekable = stream is ByteStream bs && bs.CanSeek;","typeGuard":"static bool CanSeekSafe(Stream s) => s.CanSeek;","tryCatchPattern":"try { stream.Position = 0; }\ncatch (NotSupportedException) { /* forward-only: buffer or read sequentially */ }","preventionTips":["Always check Stream.CanSeek before setting Position or calling Seek.","Buffer non-seekable streams into MemoryStream for random access.","Document that pack streams from pipes/devices are forward-only."],"tags":["wpf","io","stream","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"}