dotnet/wpf · error · ArgumentException

Cannot create a read-only stream.

Error message

Cannot create a read-only stream.

What it means

CreateTimeReadOnlyCheck rejects creating a stream with FileAccess.Read, because creating a brand-new stream that is read-only makes no sense — new streams have no content to read. An ArgumentException is thrown when GetStream/Create is asked to create a stream whose access is FileAccess.Read.

Solutions

  1. Use FileAccess.ReadWrite or FileAccess.Write when creating a stream
  2. To read an existing stream, call GetStream with FileAccess.Read which opens rather than creates
  3. Guard the access parameter before calling Create

Example fix

// before
var stream = streamInfo.Create(name, FileAccess.Read); // throws
// after
var stream = streamInfo.Create(name, FileAccess.ReadWrite);
Defensive patterns

Strategy: validation

Validate before calling

if (access == FileAccess.Read)
    throw new ArgumentException("Cannot create a read-only stream; use GetStream to open existing streams.");

Try / catch

try { streamInfo.Create(name, FileMode.Create, access); }
catch (ArgumentException ex) when (ex.Message.Contains("read-only")) { stream = streamInfo.GetStream(name, FileMode.Open, FileAccess.Read); }

Prevention

When it happens

Trigger: Calling StreamInfo.Create(streamName, FileAccess.Read) or GetStream with mode/access combination that goes down the create path with FileAccess.Read on the new stream.

Common situations: Copy-paste code passing FileAccess.Read to a create call; confusion between open-existing (read allowed) and create-new (read-only meaningless) semantics.

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

Appendix: source

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

                    StreamReference, rawStream);
            }
        }

        /// <summary>
        /// A check against FileAccess.Read at create time.  It should fail if
        ///  the root container is read-only, or if we're pointlessly trying
        ///  to create a read-only stream.
        /// </summary>
        private void CreateTimeReadOnlyCheck( FileAccess access )
        {
            // Can't create a stream if the root container is read-only
            if( FileAccess.Read == parentStorage.Root.OpenAccess )
                throw new IOException(
                    SR.CanNotCreateInReadOnly);

            // Doesn't make sense to create a new stream just to make it read-only
            if( access == FileAccess.Read )
                throw new ArgumentException(
                    SR.CanNotCreateAsReadOnly);
        }

        /// <summary>
        /// Shortcut macro - calls the IStorage::CreateStream method on the parent
        /// storage object.
        /// </summary>
        private IStream CreateStreamOnParentIStorage(
            string name, 
            int mode )
        {
            IStream createdStream = null;
            int nativeCallErrorCode = 0;

            if( !parentStorage.Exists )
            {
                parentStorage.Create();
            }

View on GitHub (pinned to 81131a70a4)