{"record":{"id":"0d1a78129b77fd08","repo":"dotnet/wpf","slug":"sr-reachpackaging-cannotmodifyreadonlycontainer","errorCode":null,"errorMessage":"SR.ReachPackaging_CannotModifyReadOnlyContainer","messagePattern":"SR\\.ReachPackaging_CannotModifyReadOnlyContainer","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsManager.cs","lineNumber":1462,"sourceCode":"        internal\n        static\n        void\n        SetXpsDocumentStartingPart(\n            Package         package,\n            PackagePart     startingPart\n            )\n        {\n            Debug.Assert(package != null, \"package cannot be null\");\n\n            //\n            // null is a valid value for startingPart; it will effectively remove starting part\n            // relationship to the existing starting Part; However, the existing startingPart\n            // won't be removed from the package\n            //\n\n            if (package.FileOpenAccess == FileAccess.Read)\n            {\n                throw new IOException(SR.ReachPackaging_CannotModifyReadOnlyContainer);\n            }\n\n            //\n            // Throw If the part provided is null\n            //\n            ArgumentNullException.ThrowIfNull(startingPart);\n\n            //\n            // Throw If the part provided is from a different container\n            //\n            if (startingPart.Package != package)\n            {\n                throw new ArgumentException(SR.ReachPackaging_PartFromDifferentContainer);\n            }\n\n                package.CreateRelationship(startingPart.Uri, TargetMode.Internal, XpsS0Markup.ReachPackageStartingPartRelationshipType);\n        }\n","sourceCodeStart":1444,"sourceCodeEnd":1480,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsManager.cs#L1444-L1480","documentation":"The static method in XpsManager that writes a starting-part relationship checks package.FileOpenAccess first; if the package was opened with FileAccess.Read, it throws IOException(SR.ReachPackaging_CannotModifyReadOnlyContainer) because establishing a new starting-part relationship mutates the package.","triggerScenarios":"Calling the XpsManager API that registers/replaces a starting part (SetStartingPart-style path) on a package opened read-only, i.e. XpsDocument opened with FileAccess.Read or FileOpenAccess.Read.","commonSituations":"Loading an XPS document from a read-only stream or file (e.g. a resource or network share) and then attempting to modify its structure; viewing code that inadvertently calls a mutating API on a read-open document.","solutions":["Open the XpsDocument/package with FileAccess.ReadWrite so the container is writable.","Do not call starting-part modification APIs when the package is read-only; skip the modification.","If the source is read-only, copy the package to a writable stream/file first, then open ReadWrite and modify."],"exampleFix":"// before\nvar doc = new XpsDocument(path, FileAccess.Read);\nSetStartingPart(doc.Package, startingPart);\n// after\nvar doc = new XpsDocument(path, FileAccess.ReadWrite);\nSetStartingPart(doc.Package, startingPart);","handlingStrategy":"type-guard","validationCode":"if (package.FileOpenAccess == FileAccess.Read) { /* skip modification or reopen writable */ }","typeGuard":"bool CanModify(Package p) => p.FileOpenAccess != FileAccess.Read;","tryCatchPattern":"catch (IOException) { /* package is read-only: reopen with FileAccess.ReadWrite */ }","preventionTips":["Open XpsDocument with FileAccess.ReadWrite whenever structure changes are planned.","Copy read-only sources to a writable location before editing.","Keep a clear read-mode vs write-mode split in your code paths."],"tags":["xps","packaging","read-only","file-access","wpf"],"backgroundTag":"permission-denied","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}