dotnet/wpf · error · IOException

Cannot open data stream.

Error message

Cannot open data stream.

What it means

OpenStreamOnParentIStorage calls native IStorage.OpenStream; any non-S_OK result throws IOException with 'Cannot open data stream.' wrapping the HRESULT in a COMException. This is the generic open failure for a named stream inside the compound file.

Solutions

  1. Check StreamInfo.Exists / InternalExists before calling GetStream
  2. Close other handles to the file that may hold a conflicting open
  3. Inspect the inner COMException HRESULT (e.g. STG_E_FILENOTFOUND vs sharing violation)
  4. If the file is corrupt, restore from backup or repair the package

Example fix

// before
var s = streamInfo.GetStream(name, FileMode.Open, FileAccess.Read); // throws if absent
// after
if (streamInfo.Exists(name))
    var s = streamInfo.GetStream(name, FileMode.Open, FileAccess.Read);
else
    Log($"Stream '{name}' not present");
Defensive patterns

Strategy: try-catch

Validate before calling

if (!streamInfo.Exists(name)) return null; // or create it instead

Try / catch

try { stream = streamInfo.GetStream(name, FileMode.Open, FileAccess.Read); }
catch (IOException ex) when (ex.InnerException is COMException ce && ce.HResult == unchecked((int)0x80030002)) { /* STG_E_FILENOTFOUND: stream missing */ }

Prevention

When it happens

Trigger: Calling StreamInfo.GetStream for a stream whose underlying IStorage::OpenStream fails: stream absent, access conflict (file opened elsewhere), corrupted storage, or insufficient access rights.

Common situations: Requesting a stream name that does not exist (typo, part removed); opening a package twice (second open fails with sharing violation); reading a damaged .docx/.xlsx.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs:638

        /// storage object.
        /// </summary>
        private IStream OpenStreamOnParentIStorage(
            string name, 
            int mode )
        {
            IStream openedStream = null;
            int nativeCallErrorCode = 0;

            nativeCallErrorCode = parentStorage.SafeIStorage.OpenStream(
                name,
                0,
                mode,
                0,
                out openedStream );

            if( SafeNativeCompoundFileConstants.S_OK != nativeCallErrorCode )
            {
                throw new IOException(
                    SR.UnableToOpenStream,
                    new COMException( 
                        SR.Format(SR.NamedAPIFailure, "IStorage.OpenStream"),
                        nativeCallErrorCode ));
            }
            return openedStream;
        }

        /// <summary>
        /// Deletes the stream specified by this StreamInfo
        /// </summary>
        internal void Delete()
        {
            CheckDisposedStatus();
        
            if( InternalExists() )
            {
                if( null != core.safeIStream )

View on GitHub (pinned to 81131a70a4)