dotnet/wpf · error · IOException
SR.StreamAlreadyExist
Error message
SR.StreamAlreadyExist
What it means
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.
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.
Example fix
// before
streamInfo.Create(content, FileAccess.Write, FileMode.CreateNew); // throws if exists
// after
try { streamInfo.Create(content, FileAccess.Write, FileMode.CreateNew); }
catch (IOException) { /* already exists: open instead */ }
// or simply overwrite:
streamInfo.Create(content, FileAccess.Write, FileMode.Create); Defensive patterns
Strategy: try-catch
Validate before calling
bool exists = false;
try { streamInfo.GetStream(FileMode.Open, FileAccess.Read); exists = true; } catch { } Try / catch
try { streamInfo.Create(content, access, FileMode.CreateNew); }
catch (IOException) { /* stream exists: open, skip, or delete then recreate */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
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
- Cannot create a stream in a read-only package.
- Cannot create data stream.
- Cannot open data stream.
- Cannot perform this function on a stream that does not…
- FileMode value is not valid.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/d1ae8f88895077c7.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs:499
core.exposedStream = null;
if( null != core.safeIStream )
{
// Release reference
((IDisposable) core.safeIStream).Dispose();
core.safeIStream = null;
}
// Cleanup done, create new stream in its place.
grfMode |= SafeNativeCompoundFileConstants.STGM_CREATE;
createdSafeIStream = CreateStreamOnParentIStorage(
core.streamName,
grfMode );
break;
case FileMode.CreateNew:
// If we've created a CFStream, this fails because stream is already there.
if( null != core.safeIStream )
throw new IOException(
SR.StreamAlreadyExist);
// Need to call Create API with NULL create flags
createdSafeIStream = CreateStreamOnParentIStorage(
core.streamName,
grfMode );
break;
case FileMode.Append: // None of these are valid in a Create
case FileMode.Open:
case FileMode.OpenOrCreate:
case FileMode.Truncate:
default:
throw new ArgumentException(
SR.FileModeInvalid);
}
core.safeIStream = createdSafeIStream;
// At this point we passed all previous checks and got the underlying IStream.View on GitHub (pinned to 81131a70a4)