{"record":{"id":"eeb9ec65cc880880","repo":"dotnet/wpf","slug":"sr-streamcannotbewritten","errorCode":null,"errorMessage":"SR.StreamCannotBeWritten","messagePattern":"SR\\.StreamCannotBeWritten","errorType":"exception","errorClass":"UnauthorizedAccessException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/Storage/XmlStreamStore.cs","lineNumber":363,"sourceCode":"\n        /// <summary>\n        ///     Causes any buffered data to be written to the underlying\n        ///     storage mechanism.  Gets called after each operation if\n        ///     AutoFlush is set to true.  The stream is truncated to\n        ///     the length of the data written out by the store.\n        /// </summary>\n        /// <exception cref=\"UnauthorizedAccessException\">stream cannot be written to</exception>\n        /// <exception cref=\"InvalidOperationException\">if no stream has been set on the store</exception>\n        /// <exception cref=\"ObjectDisposedException\">if object has been disposed</exception>\n        /// <seealso cref=\"AutoFlush\"/>\n        public override void Flush()\n        {\n            lock (SyncRoot)\n            {\n                CheckStatus();\n                if (!_stream.CanWrite)\n                {\n                    throw new UnauthorizedAccessException(SR.StreamCannotBeWritten);\n                }\n\n                if (_dirty)\n                {\n                    SerializeAnnotations();\n                    _stream.Position = 0;\n                    _stream.SetLength(0);\n                    _document.PreserveWhitespace = true;\n                    _document.Save(_stream);\n                    _stream.Flush();\n                    _dirty = false;\n                }\n            }\n        }\n\n        /// <summary>\n        /// Returns a list of namespaces that are compatible with an  input namespace\n        /// </summary>","sourceCodeStart":345,"sourceCodeEnd":381,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/Storage/XmlStreamStore.cs#L345-L381","documentation":"Flush() throws UnauthorizedAccessException (SR.StreamCannotBeWritten) when the underlying backing Stream is not writable. The store serializes pending annotations to this stream, so a read-only stream makes flushing impossible.","triggerScenarios":"Opening a stream with FileAccess.Read / FileMode.Open + read-only FileShare, or wrapping a write-protected file stream, then constructing XmlStreamStore over it and calling Flush() after annotations are marked dirty (or explicitly).","commonSituations":"Opening the annotation store file from a read-only location or medium (CD/DVD, read-only network share); opening the file while it is locked read-only; passing a MemoryStream that was created from a read-only buffer.","solutions":["Open the stream with write access: new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite).","Check stream.CanWrite before creating/flushing the store and surface a clear error to the user.","Copy the annotation store to a writable location and reopen the store over that stream.","Remove read-only attributes or fix share/ACL permissions on the store file."],"exampleFix":"// before\nvar stream = File.OpenRead(storePath);\nvar store = new XmlStreamStore(stream);\nstore.Flush();\n\n// after\nvar stream = File.Open(storePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);\nvar store = new XmlStreamStore(stream);\nstore.Flush();","handlingStrategy":"validation","validationCode":"if (!stream.CanWrite) throw new InvalidOperationException(\"Annotation store stream is read-only; cannot flush.\");","typeGuard":null,"tryCatchPattern":"try { store.Flush(); }\ncatch (UnauthorizedAccessException) { /* prompt user to save to a writable location */ }","preventionTips":["Open store streams with FileAccess.ReadWrite, never File.OpenRead.","Check stream.CanWrite immediately after opening and before wiring up the store.","Handle read-only media and locked-file scenarios (CD, network shares) with a save-as fallback."],"tags":["io","permissions","wpf","read-only-stream"],"backgroundTag":"file-write-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"}