dotnet/wpf · error · IOException

SR.StreamAlreadyExist

Error message

SR.StreamAlreadyExist

What it means

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.

Solutions

  1. Check streamInfo.Exists / storage.GetStreamInfo(name) == null before calling CreateStream, or catch the IOException and reuse the existing stream
  2. Delete the existing stream first if replacement is intended, then create anew
  3. Generate unique names (counter/guid suffix) when writing repeated parts

Example fix

// before
storage.CreateStream("Part1", CompressionOption.NotCompressed);
// after
StreamInfo si = storage.GetStreamInfo("Part1");
if (si == null)
    si = storage.CreateStream("Part1", CompressionOption.NotCompressed);
// reuse existing si
Defensive patterns

Strategy: validation

Validate before calling

if (storage.GetStreamInfo(name) == null)
    storage.CreateStream(name, CompressionOption.NotCompressed);

Type guard

static bool StreamExists(StorageInfo s, string name) => s.GetStreamInfo(name) != null;

Try / catch

try { storage.CreateStream(name, CompressionOption.NotCompressed); }
catch (IOException) { var existing = storage.GetStreamInfo(name); /* reuse existing */ }

Prevention

When it happens

Trigger: Calling storage.CreateStream(name, ...) when a stream with the same name already exists under that storage.

Common situations: 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.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/a103a1f08f001264. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageInfo.cs:274

    /// <param name="compressionOption">CompressionOptiont</param>
    /// <param name="encryptionOption">EncryptionOption</param>
    /// <returns>Reference to new stream</returns>
    public StreamInfo CreateStream( string name, CompressionOption compressionOption, EncryptionOption encryptionOption )
    {
        CheckDisposedStatus();

        //check the arguments
        ArgumentNullException.ThrowIfNull(name);

        // Stream names: we preserve casing, but do case-insensitive comparison (Native CompoundFile API behavior)
        if (string.Equals(name, EncryptedPackageEnvelope.PackageStreamName, StringComparison.OrdinalIgnoreCase))
            throw new ArgumentException(SR.Format(SR.StreamNameNotValid,name));

        //create a new streaminfo object
        StreamInfo streamInfo = new StreamInfo(this, name, compressionOption, encryptionOption);
        if (streamInfo.InternalExists())
        {
            throw new IOException(SR.StreamAlreadyExist);
        }

        //Define the compression and encryption options in the dataspacemanager
        DataSpaceManager manager = Root.GetDataSpaceManager();
        string dataSpaceLabel = null;
            
        if (manager != null)
        {
            //case : Compression option is set. Stream need to be compressed. Define compression transform.
            //At this time, we only treat CompressionOption - Normal and None. The rest are treated as Normal
            if (compressionOption != CompressionOption.NotCompressed)
            {
                //If it is not defined already, define it.
                if (!manager.TransformLabelIsDefined(sc_compressionTransformName))
                        manager.DefineTransform(CompressionTransform.ClassTransformIdentifier, sc_compressionTransformName);                
            }
             //case : Encryption option is set. Stream need to be encrypted. Define encryption transform.
            if (encryptionOption == EncryptionOption.RightsManagement)

View on GitHub (pinned to 81131a70a4)