{"record":{"id":"a65d01d1a9335329","repo":"dotnet/wpf","slug":"sr-packagingwritenotsupported-writeableondemandstream","errorCode":null,"errorMessage":"SR.PackagingWriteNotSupported","messagePattern":"SR\\.PackagingWriteNotSupported","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/Application/WriteableOnDemandStream.cs","lineNumber":278,"sourceCode":"    }\n    #endregion Protected Methods - Stream Overrides\n\n    #region Private Methods\n    //--------------------------------------------------------------------------\n    // Private Methods\n    //--------------------------------------------------------------------------\n\n    /// <summary>\n    /// Ensures either the active stream is our writeable stream or uses the\n    /// factory delegate to get one and assign it as the active stream.\n    /// </summary>\n    /// <exception cref=\"System.IO.IOException\" />\n    /// <exception cref=\"System.NotSupportedException\" />\n    private void EnsureWritable()\n    {\n        if (!_wantedWrite)\n        {\n            throw new NotSupportedException(\n                SR.PackagingWriteNotSupported);\n        }\n\n        if (!_isActiveWriteable)\n        {\n            Stream writer = _writeableStreamFactory(_mode, _access);\n            if (writer == null)\n            {\n                throw new IOException(\n                    SR.PackagingWriteableDelegateGaveNullStream);\n            }\n\n            if (writer.Equals(this))\n            {\n                throw new IOException(\n                    SR.PackagingCircularReference);\n            }\n","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/Application/WriteableOnDemandStream.cs#L260-L296","documentation":"WriteableOnDemandStream.EnsureWritable throws NotSupportedException(PackagingWriteNotSupported) when the stream was opened without write intent (_wantedWrite is false). Streams created read-only must never receive Write or SetLength calls.","triggerScenarios":"Calling Write or SetLength on a WriteableOnDemandStream obtained in read-only mode (CanWrite == false); also writing through a stream fetched with FileAccess.Read from the XPS/packaging APIs.","commonSituations":"Opening an XPS package part read-only (e.g. reading FixedDocuments) then attempting to modify its content stream; code that ignores the stream's CanWrite property.","solutions":["Check stream.CanWrite before calling Write/SetLength and open the part with FileAccess.ReadWrite or Write when modification is needed.","Open the package/part in write mode (e.g. XpsDocument with PackageOpenMode.ReadWrite) before editing content.","If only annotating/signing, use the dedicated writeable APIs rather than writing into a read stream."],"exampleFix":"// before\nStream s = xpsDoc.GetPartStream(uri, FileAccess.Read);\ns.Write(data, 0, data.Length); // NotSupportedException\n// after\nStream s = xpsDoc.GetPartStream(uri, FileAccess.ReadWrite);\nif (s.CanWrite) s.Write(data, 0, data.Length);","handlingStrategy":"type-guard","validationCode":"if (!stream.CanWrite)\n    throw new InvalidOperationException(\"Stream is read-only; open the part with FileAccess.ReadWrite.\");","typeGuard":"bool IsWritable(Stream s) => s is { CanWrite: true };","tryCatchPattern":"try { stream.SetLength(newLen); }\ncatch (NotSupportedException)\n{ ShowMessage(\"This document was opened read-only and cannot be modified.\"); }","preventionTips":["Always check CanWrite before Write/SetLength.","Open XPS packages with ReadWrite mode when editing.","Route edits through dedicated writeable APIs."],"tags":["wpf","packaging","read-only","unsupported-operation"],"backgroundTag":"unsupported-operation","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"}