{"record":{"id":"91db18d088f516db","repo":"dotnet/wpf","slug":"stream-does-not-support-writing","errorCode":null,"errorMessage":"Stream does not support writing.","messagePattern":"Stream does not support writing\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/NetStream.cs","lineNumber":311,"sourceCode":"\n\n        /// <summary>\n        /// SetLength\n        /// </summary>\n        /// <exception cref=\"NotSupportedException\">not supported</exception>\n        public override void SetLength(long newLength)\n        {\n            throw new NotSupportedException(SR.SetLengthNotSupported);\n        }\n\n\n        /// <summary>\n        /// Write\n        /// </summary>\n        /// <exception cref=\"NotSupportedException\">not supported</exception>\n        public override void Write(byte[] buf, int offset, int count)\n        {\n            throw new NotSupportedException(SR.WriteNotSupported);\n        }\n\n\n        /// <summary>\n        /// Length\n        /// </summary>\n        public override long Length\n        {\n            get\n            {\n                CheckDisposed();\n\n                // handle ftp servers that don't return a length\n                if (_fullStreamLength < 0)\n                {\n                    // fallback for servers that refuse to provide the length of the resource\n                    checked\n                    {","sourceCodeStart":293,"sourceCodeEnd":329,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/NetStream.cs#L293-L329","documentation":"NetStream is a read-only stream used to read package parts over the network; Write is intentionally unimplemented and always throws NotSupportedException (SR.WriteNotSupported). This library throws it because the stream has no writable backing store.","triggerScenarios":"Calling Write(byte[], int, int) — or BeginWrite/any copy routine that writes — on a NetStream obtained from a package part request; thrown unconditionally.","commonSituations":"Generic stream helpers that write to both source and destination (tee/logging wrappers); code that assumed GetResponseStream-like streams were writable; switching a MemoryStream for a NetStream in shared code.","solutions":["Only read from NetStream; write through the Packaging APIs (PackagePart stream opened with FileMode.Create) instead","Check stream.CanWrite before invoking Write and route writes to a different stream","Copy the NetStream into a writable stream if mutation is required"],"exampleFix":"// before\nnetStream.Write(data, 0, data.Length); // throws\n// after\nif (netStream.CanWrite)\n    netStream.Write(data, 0, data.Length);\nelse\n    writablePartStream.Write(data, 0, data.Length);","handlingStrategy":"type-guard","validationCode":"if (!stream.CanWrite)\n    throw new InvalidOperationException(\"Target stream is read-only; open the package part for write access instead\");","typeGuard":"static bool IsWritable(Stream s) => s.CanWrite;","tryCatchPattern":"try { stream.Write(data, 0, data.Length); } catch (NotSupportedException) { // route to a writable PackagePart stream\n    part.GetStream(FileMode.Create, FileAccess.Write).Write(data, 0, data.Length); }","preventionTips":["Check Stream.CanWrite before any Write call","Write package content through PackagePart streams opened with FileAccess.Write, never through response streams","Keep read-only and writable stream types in separate code paths"],"tags":["packaging","stream","not-supported","read-only"],"backgroundTag":"unsupported-operation","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"}