{"record":{"id":"8e00e3332b68b555","repo":"dotnet/wpf","slug":"sr-unabletocreatestream","errorCode":null,"errorMessage":"SR.UnableToCreateStream","messagePattern":"SR\\.UnableToCreateStream","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs","lineNumber":337,"sourceCode":"                        //  seems ugly but this method involves the fewest number of \n                        //  managed/unmanaged transitions.\n\n                        if( !parentStorage.Exists )\n                        {\n                            parentStorage.Create();\n                        }\n                        int nativeCallErrorCode = \n                            parentStorage.SafeIStorage.CreateStream(\n                                core.streamName, \n                            grfMode,\n                            0,\n                            0,\n                            out openedIStream );\n\n                        if( SafeNativeCompoundFileConstants.S_OK != nativeCallErrorCode &&\n                            SafeNativeCompoundFileConstants.STG_E_FILEALREADYEXISTS != nativeCallErrorCode )\n                        {\n                            throw new IOException(\n                                SR.UnableToCreateStream,\n                                new COMException( \n                                    SR.Format(SR.NamedAPIFailure, \"IStorage.CreateStream\"),\n                                    nativeCallErrorCode ));\n                        }\n                        \n                        // Parent storage has changed - invalidate all standing enuemrators\n                        parentStorage.InvalidateEnumerators();\n\n                        // else - proceed with open\n                    }\n\n                    if( null == openedIStream )\n                    {\n                        // If we make it here, it means the create stream call failed\n                        //  because of a STG_E_FILEALREADYEXISTS \n                        //  or container is read-only\n                        openedIStream = OpenStreamOnParentIStorage(","sourceCodeStart":319,"sourceCodeEnd":355,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs#L319-L355","documentation":"IOException wrapping a COMException from the underlying OLE call IStorage.CreateStream. When GetStream attempts to create a stream in the compound file, the native CreateStream fails with an error other than S_OK or STG_E_FILEALREADYEXISTS (which are handled as expected outcomes). The inner COMException carries the native error code (e.g. STG_E_MEDIUMFULL, STG_E_ACCESSDENIED).","triggerScenarios":"Calling StreamInfo.GetStream with FileMode.Create/CreateNew/OpenOrCreate where IStorage.CreateStream returns an unexpected native HRESULT: disk full, access denied (file opened read-only), name invalid, or storage corruption.","commonSituations":"Writing to a compound file on a full disk; the file is opened FileAccess.Read but code attempts create; the container file is locked by another process with incompatible sharing; a stream name with characters invalid to OLE storage.","solutions":["Inspect the inner COMException's HResult to identify the native failure (e.g. STG_E_MEDIUMFULL = disk full, STG_E_ACCESSDENIED = permissions).","Open the compound file with FileAccess.Write/ReadWrite and FileShare-compatible settings.","Free disk space or move the file to a writable medium.","Retry after resolving the underlying OS-level cause; only STG_E_FILEALREADYEXISTS and S_OK are treated as normal."],"exampleFix":"// before\nPackage.Open(path, FileMode.Create, FileAccess.Read); // create path fails with UnableToCreateStream\n// after\nPackage.Open(path, FileMode.Create, FileAccess.ReadWrite);","handlingStrategy":"try-catch","validationCode":"// ensure writability before calling GetStream\nusing var fs = new FileStream(containerPath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);\nif (new FileInfo(containerPath).Length >= availableDiskBytes) throw new IOException(\"Insufficient disk space.\");","typeGuard":null,"tryCatchPattern":"try { s = streamInfo.GetStream(FileMode.Create, FileAccess.Write); }\ncatch (IOException ex) when (ex.InnerException is COMException ce) { /* inspect ce.HResult: STG_E_MEDIUMFULL, STG_E_ACCESSDENIED, ... */ }","preventionTips":["Open the package with FileAccess.ReadWrite and compatible FileShare before creating streams.","Check disk space on the target volume before large writes.","Inspect the inner COMException HRESULT to distinguish causes.","Avoid stream names with characters invalid to OLE structured storage."],"tags":["io","com-interop","compound-file","packaging","file-write-failed"],"backgroundTag":"file-write-failed","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"}