{"record":{"id":"2b20ccb64458f449","repo":"dotnet/wpf","slug":"cannot-create-data-stream","errorCode":null,"errorMessage":"Cannot create data stream.","messagePattern":"Cannot create data stream\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs","lineNumber":605,"sourceCode":"            {\n                parentStorage.Create();\n            }\n\n            nativeCallErrorCode = parentStorage.SafeIStorage.CreateStream(\n                name,\n                mode,\n                0,\n                0,\n                out createdStream );\n\n            if( SafeNativeCompoundFileConstants.STG_E_INVALIDFLAG == nativeCallErrorCode )\n            {\n                throw new ArgumentException(\n                    SR.StorageFlagsUnsupported);\n            }\n            else if ( SafeNativeCompoundFileConstants.S_OK != 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            return createdStream;\n        }\n\n        /// <summary>\n        /// Shortcut macro - calls the IStorage::OpenStream method on the parent\n        /// storage object.\n        /// </summary>\n        private IStream OpenStreamOnParentIStorage(\n            string name, ","sourceCodeStart":587,"sourceCodeEnd":623,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs#L587-L623","documentation":"If the native IStorage.CreateStream call fails with any error other than S_OK or STG_E_INVALIDFLAG, the code wraps the HRESULT in a COMException and throws IOException with 'Cannot create data stream.' This indicates the underlying structured-storage create operation failed (e.g. name conflicts, corrupted file, out of space).","triggerScenarios":"IStorage.CreateStream returns a failing HRESULT: stream name invalid, storage full, file corrupted, STG_E_FILEALREADYEXISTS-type conflicts, or media errors.","commonSituations":"Writing to a corrupt or truncated compound file; disk full while saving a package; invalid characters in stream names; file locked by another process.","solutions":["Inspect the inner COMException's HRESULT to identify the native cause","Verify the file is not corrupt — try opening it with another tool or recreate it","Ensure sufficient disk space and that the stream name contains no invalid characters","Ensure no other process holds a conflicting lock on the file"],"exampleFix":"// before\ntry { streamInfo.Create(name, FileMode.Create, FileAccess.ReadWrite); }\ncatch (IOException ex) { Log(ex); throw; }\n// after\ntry { streamInfo.Create(name, FileMode.Create, FileAccess.ReadWrite); }\ncatch (IOException ex)\n{\n    var hresult = ((COMException)ex.InnerException)?.HResult ?? -1;\n    Log($\"CreateStream failed HRESULT=0x{hresult:X}\");\n    throw;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { streamInfo.Create(name, FileMode.Create, FileAccess.ReadWrite); }\ncatch (IOException ex) when (ex.InnerException is COMException ce)\n{ Log($\"IStorage.CreateStream failed: 0x{ce.HResult:X}\"); throw; }","preventionTips":["Log the inner COMException HRESULT to diagnose native failures","Check disk space and file locks before saving packages","Validate stream names for invalid characters","Periodically verify compound files are not corrupt"],"tags":["io","com-interop","packaging"],"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-21T21:30:21.729Z"}