dotnet/wpf · error · IOException

SR.SeekFailed

Error message

SR.SeekFailed

What it means

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.

Solutions

  1. Verify the underlying data source is complete and available (file fully downloaded, device connected) before opening the package.
  2. Check that the IStream backing the ByteStream genuinely supports seeking (stat the stream / confirm it is not a purely sequential pipe).
  3. Open the package from a local, seekable Stream (e.g. FileStream over a temp file) instead of a live network IStream.
  4. Catch IOException and re-create/re-open the ByteStream rather than retrying the position set on a broken stream.

Example fix

// before
stream.Position = offset; // IOException SR.SeekFailed on flaky IStream
// after
if (!stream.CanSeek)
    throw new InvalidOperationException("Backing stream is not seekable");
try { stream.Position = offset; }
catch (IOException) { stream.Dispose(); stream = File.OpenRead(localCopyPath); stream.Position = offset; }
Defensive patterns

Strategy: try-catch

Validate before calling

if (!stream.CanSeek) throw new InvalidOperationException("Underlying stream is not seekable");

Try / catch

try { stream.Position = offset; }
catch (IOException ex) when (ex.Message.Contains("Seek")) { /* re-open stream from a local, seekable source */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs:153

            set
            {
                CheckDisposedStatus();

                if (!CanSeek)
                {
                    throw new NotSupportedException(SR.SetPositionNotSupported);
                }
                
                long seekPos = 0;

                _securitySuppressedIStream.Seek(value,
                                                      NativeMethods.STREAM_SEEK_SET,
                                                      out seekPos);

                if (value != seekPos)
                {
                    throw new IOException(SR.SeekFailed);
                }
            }
        }

        #endregion Public Properties


        //------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------
        #region Public Methods

        public override void Flush()
        {
            // deliberate overidding noop
            // do not want to do anything on a read-only stream

View on GitHub (pinned to 81131a70a4)