dotnet/wpf · error · IOException

Cannot create data stream.

Error message

Cannot create data stream.

What it means

If the native IStorage.CreateStream call fails with any error other than S_OK or STG_E_INVALIDFLAG, the code wraps the HRESULT in a COMException and throws IOException with 'Cannot create data stream.' This indicates the underlying structured-storage create operation failed (e.g. name conflicts, corrupted file, out of space).

Solutions

  1. Inspect the inner COMException's HRESULT to identify the native cause
  2. Verify the file is not corrupt — try opening it with another tool or recreate it
  3. Ensure sufficient disk space and that the stream name contains no invalid characters
  4. Ensure no other process holds a conflicting lock on the file

Example fix

// before
try { streamInfo.Create(name, FileMode.Create, FileAccess.ReadWrite); }
catch (IOException ex) { Log(ex); throw; }
// after
try { streamInfo.Create(name, FileMode.Create, FileAccess.ReadWrite); }
catch (IOException ex)
{
    var hresult = ((COMException)ex.InnerException)?.HResult ?? -1;
    Log($"CreateStream failed HRESULT=0x{hresult:X}");
    throw;
}
Defensive patterns

Strategy: try-catch

Try / catch

try { streamInfo.Create(name, FileMode.Create, FileAccess.ReadWrite); }
catch (IOException ex) when (ex.InnerException is COMException ce)
{ Log($"IStorage.CreateStream failed: 0x{ce.HResult:X}"); throw; }

Prevention

When it happens

Trigger: IStorage.CreateStream returns a failing HRESULT: stream name invalid, storage full, file corrupted, STG_E_FILEALREADYEXISTS-type conflicts, or media errors.

Common situations: Writing to a corrupt or truncated compound file; disk full while saving a package; invalid characters in stream names; file locked by another process.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


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

Appendix: source

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

            {
                parentStorage.Create();
            }

            nativeCallErrorCode = parentStorage.SafeIStorage.CreateStream(
                name,
                mode,
                0,
                0,
                out createdStream );

            if( SafeNativeCompoundFileConstants.STG_E_INVALIDFLAG == nativeCallErrorCode )
            {
                throw new ArgumentException(
                    SR.StorageFlagsUnsupported);
            }
            else if ( SafeNativeCompoundFileConstants.S_OK != nativeCallErrorCode )
            {
                throw new IOException(
                    SR.UnableToCreateStream,
                    new COMException( 
                        SR.Format(SR.NamedAPIFailure, "IStorage.CreateStream"),
                        nativeCallErrorCode ));
            }

            // Parent storage has changed - invalidate all standing enuemrators
            parentStorage.InvalidateEnumerators();
        
            return createdStream;
        }

        /// <summary>
        /// Shortcut macro - calls the IStorage::OpenStream method on the parent
        /// storage object.
        /// </summary>
        private IStream OpenStreamOnParentIStorage(
            string name, 

View on GitHub (pinned to 81131a70a4)