{"record":{"id":"8b615effcc50ffc0","repo":"dotnet/wpf","slug":"sr-cannotcreatestoragerootonnonreadablestream","errorCode":null,"errorMessage":"SR.CanNotCreateStorageRootOnNonReadableStream","messagePattern":"SR\\.CanNotCreateStorageRootOnNonReadableStream","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs","lineNumber":129,"sourceCode":"\n        if( baseStream.CanRead )\n        {\n            if( baseStream.CanWrite )\n            {\n                openFlags |= SafeNativeCompoundFileConstants.STGM_READWRITE;\n            }\n            else\n            {\n                openFlags |= SafeNativeCompoundFileConstants.STGM_READ;\n                \n                if( FileMode.Create == mode )\n                    throw new ArgumentException(\n                        SR.CanNotCreateContainerOnReadOnlyStream);\n            }\n        }\n        else\n        {\n            throw new ArgumentException(\n                SR.CanNotCreateStorageRootOnNonReadableStream);\n        }\n\n        if( FileMode.Create == mode )\n        {\n            returnValue = SafeNativeCompoundFileMethods.SafeStgCreateDocfileOnStream(\n                baseStream,\n                openFlags | SafeNativeCompoundFileConstants.STGM_CREATE,\n                out storageOnStream);\n        }\n        else if( FileMode.Open == mode )\n        {\n            returnValue = SafeNativeCompoundFileMethods.SafeStgOpenStorageOnStream(\n                baseStream,\n                openFlags,\n                out storageOnStream );\n        }\n        else","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs#L111-L147","documentation":"StorageRoot.CreateOnStream requires the supplied stream to be readable; if it is not (CanRead false), it throws ArgumentException with SR.CanNotCreateStorageRootOnNonReadableStream. Even write-mode compound file operations read the stream during open/create, so a write-only stream is rejected.","triggerScenarios":"Calling StorageRoot.CreateOnStream with a stream whose CanRead is false — e.g. a stream opened with FileAccess.Write only, or a custom stream that does not support reading.","commonSituations":"Write-only network/pipe streams, streams obtained from sinks meant only for output, custom Stream implementations lacking a working Read, FileMode.OpenOrCreate on a write-only handle.","solutions":["Open the stream with FileAccess.ReadWrite so both CanRead and CanWrite are true.","Use a local MemoryStream or temp file as the container and copy it to the write-only destination afterward.","Fix custom Stream implementations to support Read.","Check stream.CanRead before the call and give a clear user-facing message."],"exampleFix":"// before\nvar stream = new FileStream(path, FileMode.Open, FileAccess.Write);\nvar root = StorageRoot.CreateOnStream(stream, FileMode.Open, FileAccess.Write); // ArgumentException\n// after\nvar stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);\nvar root = StorageRoot.CreateOnStream(stream, FileMode.Open, FileAccess.ReadWrite);","handlingStrategy":"validation","validationCode":"if (stream == null) throw new ArgumentNullException(nameof(stream));\nif (!stream.CanRead)\n    throw new InvalidOperationException(\"CreateOnStream requires a readable stream\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check CanRead and CanWrite before creating a root on a stream.","Open underlying files with FileAccess.ReadWrite.","Ensure custom Stream implementations support Read."],"tags":["io","compound-file","stream","argument-error"],"backgroundTag":"invalid-argument-value","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"}