{"record":{"id":"f41b318e3053c123","repo":"dotnet/wpf","slug":"cannot-create-a-stream-in-a-read-only-package","errorCode":null,"errorMessage":"Cannot create a stream in a read-only package.","messagePattern":"Cannot create a stream in a read-only package\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs","lineNumber":566,"sourceCode":"            }\n            else\n            {\n                // Pass raw stream to data space manager to get real stream\n               return parentStorage.Root.GetDataSpaceManager().CreateDataSpaceStream(\n                    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;","sourceCodeStart":548,"sourceCodeEnd":584,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs#L548-L584","documentation":"StreamInfo.Create/GetStream calls CreateTimeReadOnlyCheck before creating a new stream in the compound file. If the root storage was opened with FileAccess.Read, no new stream can be created, so an IOException is thrown. This is a deliberate write-attempt-on-read-only-container guard.","triggerScenarios":"Calling StreamInfo.Create or StreamInfo.GetStream on a storage whose root was opened read-only (OpenAccess == FileAccess.Read), e.g. after StorageRoot.Open(path, FileMode.Open, FileAccess.Read) or via Package.Open with read access.","commonSituations":"Opening a .docx/.xlsx compound package in read-only mode then attempting to add a part; opening files from read-only media or a downloaded file left with read-only attribute but still calling write APIs.","solutions":["Open the root storage/Package with FileAccess.ReadWrite or FileAccess.Write instead of FileAccess.Read","Check parentStorage.Root.OpenAccess before calling Create and open a new writable instance","If only reading is intended, use GetStream instead of Create and avoid mutating APIs"],"exampleFix":"// before\nvar package = Package.Open(path, FileMode.Open, FileAccess.Read);\npackage.CreatePart(uri, mime); // throws\n// after\nvar package = Package.Open(path, FileMode.Open, FileAccess.ReadWrite);\npackage.CreatePart(uri, mime);","handlingStrategy":"validation","validationCode":"if (root.OpenAccess == FileAccess.Read)\n    throw new InvalidOperationException(\"Package opened read-only; cannot create streams.\");","typeGuard":null,"tryCatchPattern":"try { streamInfo.Create(name, FileMode.Create, FileAccess.ReadWrite); }\ncatch (IOException ex) when (ex.Message.Contains(\"read-only\")) { /* open writable instead */ }","preventionTips":["Always open packages with FileAccess.ReadWrite when any mutation is planned","Check Package.FileOpenAccess before write operations","Avoid Package.Open(path) default read mode when you intend to save"],"tags":["io","read-only","packaging"],"backgroundTag":"permission-denied","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"}