{"record":{"id":"14433b6ca489db83","repo":"dotnet/wpf","slug":"sr-unabletocreateonstream","errorCode":null,"errorMessage":"SR.UnableToCreateOnStream","messagePattern":"SR\\.UnableToCreateOnStream","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs","lineNumber":160,"sourceCode":"            returnValue = SafeNativeCompoundFileMethods.SafeStgOpenStorageOnStream(\n                baseStream,\n                openFlags,\n                out storageOnStream );\n        }\n        else\n        {\n            throw new ArgumentException(\n                SR.CreateModeMustBeCreateOrOpen);\n        }\n        \n        switch( (uint) returnValue )\n        {\n            case SafeNativeCompoundFileConstants.S_OK:\n                return StorageRoot.CreateOnIStorage( \n                    storageOnStream );\n\n            default:\n                throw new IOException(\n                    SR.UnableToCreateOnStream,\n                    new COMException(\n                        SR.CFAPIFailure, \n                        returnValue));\n        }\n    }\n\n    /// <summary>Open a container, given only the path.</summary>\n    /// <param name=\"path\">Path to container file on local file system</param>\n    /// <returns>StorageRoot instance representing the file</returns>\n    internal static StorageRoot Open( \n        string path )\n    {\n        return Open( path, defaultFileMode, defaultFileAccess, defaultFileShare, defaultSectorSize );\n    }\n\n    /// <summary>Open a container, given path and open mode</summary>\n    /// <param name=\"path\">Path to container file on local file system</param>","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs#L142-L178","documentation":"StorageRoot.CreateOnStream failed to create a compound file container on the given Stream: the underlying StgCreateStorageEx-on-stream call returned a failure HRESULT other than S_OK. The library wraps the HRESULT in a COMException and surfaces it as an IOException because the compound file could not be initialized on the supplied stream.","triggerScenarios":"Calling System.IO.Packaging.StorageRoot.CreateOnStream (indirectly via Package.Open on a stream with Create mode) when the native compound-file API returns an error, e.g. the stream does not support Read/Write/Seek as required, or the stream is closed/disposed.","commonSituations":"Passing a non-seekable or write-protected stream to Package.Open; using a MemoryStream already closed; opening a package on a stream over a read-only file while requesting Create mode.","solutions":["Ensure the Stream supports Read, Write and Seek (CanRead/CanWrite/CanSeek all true) before creating the package.","Check the inner COMException's HResult to identify the native failure and fix the root cause (permissions, stream state).","Verify the stream is open (not disposed) for the lifetime of the package.","If creating on disk is acceptable, use the path-based Package.Open overload instead of a stream."],"exampleFix":"// before\nvar pkg = Package.Open(closedOrReadOnlyStream, FileMode.Create);\n// after\nif (!stream.CanRead || !stream.CanWrite || !stream.CanSeek)\n    stream = new MemoryStream();\nvar pkg = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite);","handlingStrategy":"validation","validationCode":"if (stream == null) throw new ArgumentNullException(nameof(stream));\nif (!stream.CanRead || !stream.CanWrite || !stream.CanSeek)\n    throw new ArgumentException(\"Stream must be readable, writable and seekable\", nameof(stream));","typeGuard":"bool IsValidPackageStream(Stream s) => s != null && s.CanRead && s.CanWrite && s.CanSeek;","tryCatchPattern":null,"preventionTips":["Always pass seekable read/write streams (FileStream, MemoryStream) to package APIs.","Keep the stream open for the lifetime of the Package.","Check inner COMException HResult when an IOException surfaces from package creation."],"tags":["io","compound-file","stream"],"backgroundTag":"file-open-failed","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"}