{"record":{"id":"a103a1f08f001264","repo":"dotnet/wpf","slug":"sr-streamalreadyexist","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/StorageInfo.cs","lineNumber":274,"sourceCode":"    /// <param name=\"compressionOption\">CompressionOptiont</param>\n    /// <param name=\"encryptionOption\">EncryptionOption</param>\n    /// <returns>Reference to new stream</returns>\n    public StreamInfo CreateStream( string name, CompressionOption compressionOption, EncryptionOption encryptionOption )\n    {\n        CheckDisposedStatus();\n\n        //check the arguments\n        ArgumentNullException.ThrowIfNull(name);\n\n        // Stream names: we preserve casing, but do case-insensitive comparison (Native CompoundFile API behavior)\n        if (string.Equals(name, EncryptedPackageEnvelope.PackageStreamName, StringComparison.OrdinalIgnoreCase))\n            throw new ArgumentException(SR.Format(SR.StreamNameNotValid,name));\n\n        //create a new streaminfo object\n        StreamInfo streamInfo = new StreamInfo(this, name, compressionOption, encryptionOption);\n        if (streamInfo.InternalExists())\n        {\n            throw new IOException(SR.StreamAlreadyExist);\n        }\n\n        //Define the compression and encryption options in the dataspacemanager\n        DataSpaceManager manager = Root.GetDataSpaceManager();\n        string dataSpaceLabel = null;\n            \n        if (manager != null)\n        {\n            //case : Compression option is set. Stream need to be compressed. Define compression transform.\n            //At this time, we only treat CompressionOption - Normal and None. The rest are treated as Normal\n            if (compressionOption != CompressionOption.NotCompressed)\n            {\n                //If it is not defined already, define it.\n                if (!manager.TransformLabelIsDefined(sc_compressionTransformName))\n                        manager.DefineTransform(CompressionTransform.ClassTransformIdentifier, sc_compressionTransformName);                \n            }\n             //case : Encryption option is set. Stream need to be encrypted. Define encryption transform.\n            if (encryptionOption == EncryptionOption.RightsManagement)","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageInfo.cs#L256-L292","documentation":"StorageInfo.CreateStream checks whether a stream of the given name already exists in the parent storage and throws IOException (SR.StreamAlreadyExist) if so. Compound-file streams are unique per storage, so duplicate creation is rejected rather than silently overwritten.","triggerScenarios":"Calling storage.CreateStream(name, ...) when a stream with the same name already exists under that storage.","commonSituations":"Re-running package-generation code without clearing the container; appending parts to an existing XPS/OPC file that already contains those parts; race conditions where two writes use the same part name.","solutions":["Check streamInfo.Exists / storage.GetStreamInfo(name) == null before calling CreateStream, or catch the IOException and reuse the existing stream","Delete the existing stream first if replacement is intended, then create anew","Generate unique names (counter/guid suffix) when writing repeated parts"],"exampleFix":"// before\nstorage.CreateStream(\"Part1\", CompressionOption.NotCompressed);\n// after\nStreamInfo si = storage.GetStreamInfo(\"Part1\");\nif (si == null)\n    si = storage.CreateStream(\"Part1\", CompressionOption.NotCompressed);\n// reuse existing si","handlingStrategy":"validation","validationCode":"if (storage.GetStreamInfo(name) == null)\n    storage.CreateStream(name, CompressionOption.NotCompressed);","typeGuard":"static bool StreamExists(StorageInfo s, string name) => s.GetStreamInfo(name) != null;","tryCatchPattern":"try { storage.CreateStream(name, CompressionOption.NotCompressed); }\ncatch (IOException) { var existing = storage.GetStreamInfo(name); /* reuse existing */ }","preventionTips":["Treat compound-file streams as create-once; look up before creating","Clear or recreate the container when re-running generation code","Use unique part names (guid/counter) for repeated writes"],"tags":["io","duplicate","file-already-exists","compound-file","stream"],"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-22T01:17:13.364Z"}