dotnet/wpf · error · ArgumentException

SR.CanNotCreateStorageRootOnNonReadableStream

Error message

SR.CanNotCreateStorageRootOnNonReadableStream

What it means

StorageRoot.CreateOnStream requires the supplied stream to be readable; if it is not (CanRead false), it throws ArgumentException with SR.CanNotCreateStorageRootOnNonReadableStream. Even write-mode compound file operations read the stream during open/create, so a write-only stream is rejected.

Solutions

  1. Open the stream with FileAccess.ReadWrite so both CanRead and CanWrite are true.
  2. Use a local MemoryStream or temp file as the container and copy it to the write-only destination afterward.
  3. Fix custom Stream implementations to support Read.
  4. Check stream.CanRead before the call and give a clear user-facing message.

Example fix

// before
var stream = new FileStream(path, FileMode.Open, FileAccess.Write);
var root = StorageRoot.CreateOnStream(stream, FileMode.Open, FileAccess.Write); // ArgumentException
// after
var stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
var root = StorageRoot.CreateOnStream(stream, FileMode.Open, FileAccess.ReadWrite);
Defensive patterns

Strategy: validation

Validate before calling

if (stream == null) throw new ArgumentNullException(nameof(stream));
if (!stream.CanRead)
    throw new InvalidOperationException("CreateOnStream requires a readable stream");

Prevention

When it happens

Trigger: Calling StorageRoot.CreateOnStream with a stream whose CanRead is false — e.g. a stream opened with FileAccess.Write only, or a custom stream that does not support reading.

Common situations: Write-only network/pipe streams, streams obtained from sinks meant only for output, custom Stream implementations lacking a working Read, FileMode.OpenOrCreate on a write-only handle.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs:129

        if( baseStream.CanRead )
        {
            if( baseStream.CanWrite )
            {
                openFlags |= SafeNativeCompoundFileConstants.STGM_READWRITE;
            }
            else
            {
                openFlags |= SafeNativeCompoundFileConstants.STGM_READ;
                
                if( FileMode.Create == mode )
                    throw new ArgumentException(
                        SR.CanNotCreateContainerOnReadOnlyStream);
            }
        }
        else
        {
            throw new ArgumentException(
                SR.CanNotCreateStorageRootOnNonReadableStream);
        }

        if( FileMode.Create == mode )
        {
            returnValue = SafeNativeCompoundFileMethods.SafeStgCreateDocfileOnStream(
                baseStream,
                openFlags | SafeNativeCompoundFileConstants.STGM_CREATE,
                out storageOnStream);
        }
        else if( FileMode.Open == mode )
        {
            returnValue = SafeNativeCompoundFileMethods.SafeStgOpenStorageOnStream(
                baseStream,
                openFlags,
                out storageOnStream );
        }
        else

View on GitHub (pinned to 81131a70a4)