dotnet/wpf · error · IOException

SR.UnableToCreateStream

Error message

SR.UnableToCreateStream

What it means

IOException wrapping a COMException from the underlying OLE call IStorage.CreateStream. When GetStream attempts to create a stream in the compound file, the native CreateStream fails with an error other than S_OK or STG_E_FILEALREADYEXISTS (which are handled as expected outcomes). The inner COMException carries the native error code (e.g. STG_E_MEDIUMFULL, STG_E_ACCESSDENIED).

Solutions

  1. Inspect the inner COMException's HResult to identify the native failure (e.g. STG_E_MEDIUMFULL = disk full, STG_E_ACCESSDENIED = permissions).
  2. Open the compound file with FileAccess.Write/ReadWrite and FileShare-compatible settings.
  3. Free disk space or move the file to a writable medium.
  4. Retry after resolving the underlying OS-level cause; only STG_E_FILEALREADYEXISTS and S_OK are treated as normal.

Example fix

// before
Package.Open(path, FileMode.Create, FileAccess.Read); // create path fails with UnableToCreateStream
// after
Package.Open(path, FileMode.Create, FileAccess.ReadWrite);
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure writability before calling GetStream
using var fs = new FileStream(containerPath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
if (new FileInfo(containerPath).Length >= availableDiskBytes) throw new IOException("Insufficient disk space.");

Try / catch

try { s = streamInfo.GetStream(FileMode.Create, FileAccess.Write); }
catch (IOException ex) when (ex.InnerException is COMException ce) { /* inspect ce.HResult: STG_E_MEDIUMFULL, STG_E_ACCESSDENIED, ... */ }

Prevention

When it happens

Trigger: Calling StreamInfo.GetStream with FileMode.Create/CreateNew/OpenOrCreate where IStorage.CreateStream returns an unexpected native HRESULT: disk full, access denied (file opened read-only), name invalid, or storage corruption.

Common situations: Writing to a compound file on a full disk; the file is opened FileAccess.Read but code attempts create; the container file is locked by another process with incompatible sharing; a stream name with characters invalid to OLE storage.

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/8e00e3332b68b555. Report an issue: GitHub.

Appendix: source

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

                        //  seems ugly but this method involves the fewest number of 
                        //  managed/unmanaged transitions.

                        if( !parentStorage.Exists )
                        {
                            parentStorage.Create();
                        }
                        int nativeCallErrorCode = 
                            parentStorage.SafeIStorage.CreateStream(
                                core.streamName, 
                            grfMode,
                            0,
                            0,
                            out openedIStream );

                        if( SafeNativeCompoundFileConstants.S_OK != nativeCallErrorCode &&
                            SafeNativeCompoundFileConstants.STG_E_FILEALREADYEXISTS != 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();

                        // else - proceed with open
                    }

                    if( null == openedIStream )
                    {
                        // If we make it here, it means the create stream call failed
                        //  because of a STG_E_FILEALREADYEXISTS 
                        //  or container is read-only
                        openedIStream = OpenStreamOnParentIStorage(

View on GitHub (pinned to 81131a70a4)