{"record":{"id":"d1ae8f88895077c7","repo":"dotnet/wpf","slug":"sr-streamalreadyexist-streaminfo","errorCode":null,"errorMessage":"SR.StreamAlreadyExist","messagePattern":"SR\\.StreamAlreadyExist","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs","lineNumber":499,"sourceCode":"                    core.exposedStream = null;\n\n                    if( null != core.safeIStream )\n                    {\n                        // Release reference\n                        ((IDisposable) core.safeIStream).Dispose();\n                        core.safeIStream = null;\n                    }\n\n                    // Cleanup done, create new stream in its place.\n                    grfMode |= SafeNativeCompoundFileConstants.STGM_CREATE;\n                    createdSafeIStream = CreateStreamOnParentIStorage(\n                            core.streamName, \n                        grfMode );    \n                    break;\n                case FileMode.CreateNew:\n                    // If we've created a CFStream, this fails because stream is already there.\n                    if( null != core.safeIStream )\n                        throw new IOException(\n                            SR.StreamAlreadyExist);\n\n                    // Need to call Create API with NULL create flags\n                    createdSafeIStream = CreateStreamOnParentIStorage(\n                            core.streamName, \n                        grfMode );\n                    break;\n                case FileMode.Append:   // None of these are valid in a Create\n                case FileMode.Open:\n                case FileMode.OpenOrCreate:\n                case FileMode.Truncate:\n                default:\n                    throw new ArgumentException(\n                        SR.FileModeInvalid);\n            }\n\n            core.safeIStream = createdSafeIStream;\n            // At this point we passed all previous checks and got the underlying IStream.","sourceCodeStart":481,"sourceCodeEnd":517,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs#L481-L517","documentation":"IOException thrown by StreamInfo.Create when FileMode.CreateNew is requested but a stream with this name already exists in the parent storage (StreamInfo detected a cached underlying stream, core.safeIStream != null). CreateNew semantics require the target not to exist, so the library fails rather than overwriting. This mirrors the classic FileExists failure for the compound-file world.","triggerScenarios":"Calling streamInfo.Create(..., FileMode.CreateNew) on a StreamInfo that already wraps an existing/open stream; calling Create twice on the same StreamInfo without deleting the stream.","commonSituations":"Re-running initialization code that creates fixed-name package streams; duplicate resource names (e.g. two parts with the same URI in a package); idempotency logic missing around stream creation.","solutions":["Check existence first (e.g. try Open with FileMode.Open, or track created streams) and skip creation if present.","Use FileMode.Create instead if overwriting the existing stream is acceptable.","Delete the existing stream (StreamInfo.Delete) before CreateNew.","Ensure Create is only called once per StreamInfo instance."],"exampleFix":"// before\nstreamInfo.Create(content, FileAccess.Write, FileMode.CreateNew); // throws if exists\n// after\ntry { streamInfo.Create(content, FileAccess.Write, FileMode.CreateNew); }\ncatch (IOException) { /* already exists: open instead */ }\n// or simply overwrite:\nstreamInfo.Create(content, FileAccess.Write, FileMode.Create);","handlingStrategy":"try-catch","validationCode":"bool exists = false;\ntry { streamInfo.GetStream(FileMode.Open, FileAccess.Read); exists = true; } catch { }","typeGuard":null,"tryCatchPattern":"try { streamInfo.Create(content, access, FileMode.CreateNew); }\ncatch (IOException) { /* stream exists: open, skip, or delete then recreate */ }","preventionTips":["Make stream creation idempotent: check-then-create or catch the IOException.","Use FileMode.Create when overwrite is acceptable.","Deduplicate resource names before creating package parts.","Delete stale streams before re-running initialization."],"tags":["io","conflict","filemode","packaging","already-exists"],"backgroundTag":"file-already-exists","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"}