dotnet/wpf · error · ArgumentException
FileMode value is not valid.
Error message
FileMode value is not valid.
What it means
ArgumentException thrown by StreamInfo.Create for FileModes that make no sense when creating a new stream: Append, Open, OpenOrCreate, Truncate, and the default catch-all for invalid enum values. Unlike GetStream, Create only accepts FileMode.Create and FileMode.CreateNew; everything else is rejected with SR.FileModeInvalid. The message text is 'FileMode value is not valid.'
Solutions
- Pass only FileMode.Create or FileMode.CreateNew to StreamInfo.Create.
- Route open-existing scenarios to StreamInfo.GetStream (or streamInfo.Stream) instead of Create.
- Validate/whitelist the FileMode before calling Create.
- Fix config/serialization sources that supply arbitrary FileMode values.
Example fix
// before
streamInfo.Create(content, FileAccess.Write, FileMode.OpenOrCreate);
// after
FileMode mode = /* ... */;
if (mode != FileMode.Create && mode != FileMode.CreateNew)
throw new ArgumentException("StreamInfo.Create only accepts Create or CreateNew.");
streamInfo.Create(content, FileAccess.Write, mode); Defensive patterns
Strategy: validation
Validate before calling
if (mode != FileMode.Create && mode != FileMode.CreateNew) throw new ArgumentException("StreamInfo.Create accepts only FileMode.Create or FileMode.CreateNew."); Type guard
bool IsValidCreateMode(FileMode m) => m == FileMode.Create || m == FileMode.CreateNew;
Try / catch
try { streamInfo.Create(content, access, mode); }
catch (ArgumentException) { streamInfo.Create(content, access, FileMode.Create); } Prevention
- Remember Create accepts only Create/CreateNew; route other modes to GetStream.
- Don't share one FileMode parameter between GetStream and Create call sites.
- Whitelist FileModes in helper methods rather than forwarding raw input.
When it happens
Trigger: Calling streamInfo.Create with FileMode.Open/OpenOrCreate/Append/Truncate, or with an out-of-range value cast to FileMode; reusing a FileMode chosen for GetStream in a Create call.
Common situations: Shared helper methods that take a FileMode parameter and pass it to both GetStream and Create; config-driven FileModes; copy-paste between the two APIs.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- SR.FileModeInvalid
- Cannot have leading path delimiter.
- CompoundFile path must be non-empty.
- SR.CompoundFilePathNullEmpty
- SR.DataSpaceLabelInvalidEmpty
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/df477fe064e320b3.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs:512
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.
// Set our data space label to the given label, and the stream to the retrieved stream.
core.dataSpaceLabel = dataSpace;
if( null != dataSpace )
{
dataSpaceManager.CreateDataSpaceMapping(
new CompoundFileStreamReference( parentStorage.FullNameInternal, core.streamName ),
core.dataSpaceLabel );
}
Stream returnStream =
BuildStreamOnUnderlyingIStream( core.safeIStream, openFileAccess, this );
_needToGetTransformInfo = false; // We created stream with the given dataspace settingView on GitHub (pinned to 81131a70a4)