{"record":{"id":"20a01d68fa4eea0e","repo":"dotnet/wpf","slug":"cannot-open-data-stream","errorCode":null,"errorMessage":"Cannot open data stream.","messagePattern":"Cannot open data stream\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs","lineNumber":638,"sourceCode":"        /// storage object.\n        /// </summary>\n        private IStream OpenStreamOnParentIStorage(\n            string name, \n            int mode )\n        {\n            IStream openedStream = null;\n            int nativeCallErrorCode = 0;\n\n            nativeCallErrorCode = parentStorage.SafeIStorage.OpenStream(\n                name,\n                0,\n                mode,\n                0,\n                out openedStream );\n\n            if( SafeNativeCompoundFileConstants.S_OK != nativeCallErrorCode )\n            {\n                throw new IOException(\n                    SR.UnableToOpenStream,\n                    new COMException( \n                        SR.Format(SR.NamedAPIFailure, \"IStorage.OpenStream\"),\n                        nativeCallErrorCode ));\n            }\n            return openedStream;\n        }\n\n        /// <summary>\n        /// Deletes the stream specified by this StreamInfo\n        /// </summary>\n        internal void Delete()\n        {\n            CheckDisposedStatus();\n        \n            if( InternalExists() )\n            {\n                if( null != core.safeIStream )","sourceCodeStart":620,"sourceCodeEnd":656,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs#L620-L656","documentation":"OpenStreamOnParentIStorage calls native IStorage.OpenStream; any non-S_OK result throws IOException with 'Cannot open data stream.' wrapping the HRESULT in a COMException. This is the generic open failure for a named stream inside the compound file.","triggerScenarios":"Calling StreamInfo.GetStream for a stream whose underlying IStorage::OpenStream fails: stream absent, access conflict (file opened elsewhere), corrupted storage, or insufficient access rights.","commonSituations":"Requesting a stream name that does not exist (typo, part removed); opening a package twice (second open fails with sharing violation); reading a damaged .docx/.xlsx.","solutions":["Check StreamInfo.Exists / InternalExists before calling GetStream","Close other handles to the file that may hold a conflicting open","Inspect the inner COMException HRESULT (e.g. STG_E_FILENOTFOUND vs sharing violation)","If the file is corrupt, restore from backup or repair the package"],"exampleFix":"// before\nvar s = streamInfo.GetStream(name, FileMode.Open, FileAccess.Read); // throws if absent\n// after\nif (streamInfo.Exists(name))\n    var s = streamInfo.GetStream(name, FileMode.Open, FileAccess.Read);\nelse\n    Log($\"Stream '{name}' not present\");","handlingStrategy":"try-catch","validationCode":"if (!streamInfo.Exists(name)) return null; // or create it instead","typeGuard":null,"tryCatchPattern":"try { stream = streamInfo.GetStream(name, FileMode.Open, FileAccess.Read); }\ncatch (IOException ex) when (ex.InnerException is COMException ce && ce.HResult == unchecked((int)0x80030002)) { /* STG_E_FILENOTFOUND: stream missing */ }","preventionTips":["Check Exists before opening","Don't open the same package twice; share a single root","Handle sharing violations by retrying after releasing other handles"],"tags":["io","file-not-found","packaging"],"backgroundTag":"file-not-found","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"}