dotnet/wpf · error · ArgumentException

SR.CanNotCreateContainerOnReadOnlyStream

Error message

SR.CanNotCreateContainerOnReadOnlyStream

What it means

StorageRoot.CreateOnStream builds STGM flags from the caller's FileAccess; when the stream is not writable it adds STGM_READ and, if FileMode.Create was requested, throws ArgumentException with SR.CanNotCreateContainerOnReadOnlyStream. Creating a compound-file root requires a writable target, so Create on a read-only stream is rejected up front.

Solutions

  1. Open the underlying stream with FileAccess.ReadWrite (or Write) before CreateOnStream.
  2. If the source must stay read-only, copy it to a MemoryStream/(temp) file and create the root on the writable copy.
  3. Use FileMode.Open or OpenOrCreate instead of Create when you only intend to read an existing container.
  4. Check stream.CanWrite before calling and surface a clear error to the user.

Example fix

// before
var stream = File.OpenRead(path);
var root = StorageRoot.CreateOnStream(stream, FileMode.Create, FileAccess.ReadWrite); // ArgumentException
// after
var stream = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
var root = StorageRoot.CreateOnStream(stream, FileMode.Create, FileAccess.ReadWrite);
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Calling StorageRoot.CreateOnStream(stream, FileMode.Create, access) where the stream was opened read-only (e.g. new FileStream(path, FileMode.Open, FileAccess.Read)) or CanWrite is false.

Common situations: Opening a file from a read-only resource stream, Assembly.GetManifestResourceStream (read-only), or a file on read-only media and then attempting to create a new package on it.

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

Appendix: source

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

    {
        ArgumentNullException.ThrowIfNull(baseStream);

        IStorage storageOnStream;
        int returnValue;
        int openFlags = SafeNativeCompoundFileConstants.STGM_SHARE_EXCLUSIVE;

        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 )
        {

View on GitHub (pinned to 81131a70a4)