{"record":{"id":"cf39dbfea55c115f","repo":"dotnet/wpf","slug":"sr-cannotcreatecontaineronreadonlystream","errorCode":null,"errorMessage":"SR.CanNotCreateContainerOnReadOnlyStream","messagePattern":"SR\\.CanNotCreateContainerOnReadOnlyStream","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs","lineNumber":123,"sourceCode":"    {\n        ArgumentNullException.ThrowIfNull(baseStream);\n\n        IStorage storageOnStream;\n        int returnValue;\n        int openFlags = SafeNativeCompoundFileConstants.STGM_SHARE_EXCLUSIVE;\n\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        {","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs#L105-L141","documentation":"StorageRoot.CreateOnStream builds STGM flags from the caller's FileAccess; when the stream is not writable it adds STGM_READ and, if FileMode.Create was requested, throws ArgumentException with SR.CanNotCreateContainerOnReadOnlyStream. Creating a compound-file root requires a writable target, so Create on a read-only stream is rejected up front.","triggerScenarios":"Calling StorageRoot.CreateOnStream(stream, FileMode.Create, access) where the stream was opened read-only (e.g. new FileStream(path, FileMode.Open, FileAccess.Read)) or CanWrite is false.","commonSituations":"Opening a file from a read-only resource stream, Assembly.GetManifestResourceStream (read-only), or a file on read-only media and then attempting to create a new package on it.","solutions":["Open the underlying stream with FileAccess.ReadWrite (or Write) before CreateOnStream.","If the source must stay read-only, copy it to a MemoryStream/(temp) file and create the root on the writable copy.","Use FileMode.Open or OpenOrCreate instead of Create when you only intend to read an existing container.","Check stream.CanWrite before calling and surface a clear error to the user."],"exampleFix":"// before\nvar stream = File.OpenRead(path);\nvar root = StorageRoot.CreateOnStream(stream, FileMode.Create, FileAccess.ReadWrite); // ArgumentException\n// after\nvar stream = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);\nvar root = StorageRoot.CreateOnStream(stream, FileMode.Create, FileAccess.ReadWrite);","handlingStrategy":"validation","validationCode":"if (stream == null) throw new ArgumentNullException(nameof(stream));\nif (!stream.CanWrite)\n    throw new InvalidOperationException(\"CreateOnStream requires a writable stream\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check stream.CanWrite before CreateOnStream.","Never use GetManifestResourceStream or File.OpenRead as the target of Create.","Copy read-only sources to a writable temp stream first."],"tags":["io","compound-file","read-only","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-21T21:30:21.729Z"}