{"record":{"id":"0ae68ada9a5a33e2","repo":"dotnet/wpf","slug":"cannot-create-a-read-only-stream","errorCode":null,"errorMessage":"Cannot create a read-only stream.","messagePattern":"Cannot create a read-only stream\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs","lineNumber":571,"sourceCode":"                    StreamReference, rawStream);\n            }\n        }\n\n        /// <summary>\n        /// A check against FileAccess.Read at create time.  It should fail if\n        ///  the root container is read-only, or if we're pointlessly trying\n        ///  to create a read-only stream.\n        /// </summary>\n        private void CreateTimeReadOnlyCheck( FileAccess access )\n        {\n            // Can't create a stream if the root container is read-only\n            if( FileAccess.Read == parentStorage.Root.OpenAccess )\n                throw new IOException(\n                    SR.CanNotCreateInReadOnly);\n\n            // Doesn't make sense to create a new stream just to make it read-only\n            if( access == FileAccess.Read )\n                throw new ArgumentException(\n                    SR.CanNotCreateAsReadOnly);\n        }\n\n        /// <summary>\n        /// Shortcut macro - calls the IStorage::CreateStream method on the parent\n        /// storage object.\n        /// </summary>\n        private IStream CreateStreamOnParentIStorage(\n            string name, \n            int mode )\n        {\n            IStream createdStream = null;\n            int nativeCallErrorCode = 0;\n\n            if( !parentStorage.Exists )\n            {\n                parentStorage.Create();\n            }","sourceCodeStart":553,"sourceCodeEnd":589,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs#L553-L589","documentation":"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.","triggerScenarios":"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.","commonSituations":"Copy-paste code passing FileAccess.Read to a create call; confusion between open-existing (read allowed) and create-new (read-only meaningless) semantics.","solutions":["Use FileAccess.ReadWrite or FileAccess.Write when creating a stream","To read an existing stream, call GetStream with FileAccess.Read which opens rather than creates","Guard the access parameter before calling Create"],"exampleFix":"// before\nvar stream = streamInfo.Create(name, FileAccess.Read); // throws\n// after\nvar stream = streamInfo.Create(name, FileAccess.ReadWrite);","handlingStrategy":"validation","validationCode":"if (access == FileAccess.Read)\n    throw new ArgumentException(\"Cannot create a read-only stream; use GetStream to open existing streams.\");","typeGuard":null,"tryCatchPattern":"try { streamInfo.Create(name, FileMode.Create, access); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"read-only\")) { stream = streamInfo.GetStream(name, FileMode.Open, FileAccess.Read); }","preventionTips":["Never pass FileAccess.Read to Create-type APIs","Use GetStream/FileMode.Open for read scenarios","Centralize stream creation in a helper that enforces write access"],"tags":["argument","read-only","packaging"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}