dotnet/wpf · error · ArgumentException
SR.FileModeUnsupported
Error message
SR.FileModeUnsupported
What it means
ArgumentException thrown by StreamInfo.GetStream when FileMode.Append is requested. Append mode has no meaning for OLE structured-storage streams inside a compound file, so the library rejects it up front. The caller must pick a FileMode supported by the compound file model (Create, CreateNew, Open, OpenOrCreate).
Solutions
- Replace FileMode.Append with FileMode.OpenOrCreate and seek to the end of the stream if append semantics are needed.
- Use FileMode.Open with FileAccess.Write, then seek to stream length before writing.
- Restructure the call site so append-style writes use a standard FileStream outside the compound file if applicable.
Example fix
// before Stream s = streamInfo.GetStream(FileMode.Append, FileAccess.Write); // after Stream s = streamInfo.GetStream(FileMode.OpenOrCreate, FileAccess.Write); s.Seek(0, SeekOrigin.End); // append semantics
Defensive patterns
Strategy: validation
Validate before calling
if (mode == FileMode.Append) throw new ArgumentException("FileMode.Append is not supported by CompoundFile StreamInfo.GetStream; use OpenOrCreate and seek to end."); Type guard
bool IsSupportedGetStreamMode(FileMode m) => m == FileMode.Create || m == FileMode.Open || m == FileMode.OpenOrCreate;
Try / catch
try { s = streamInfo.GetStream(mode, access); }
catch (ArgumentException) { s = streamInfo.GetStream(FileMode.OpenOrCreate, access); s.Seek(0, SeekOrigin.End); } Prevention
- Whitelist FileModes per API: GetStream supports Create/Open/OpenOrCreate only.
- Never pass through user/config-supplied FileModes unvalidated.
- Implement append by seeking to end after OpenOrCreate.
When it happens
Trigger: Calling streamInfo.GetStream(FileMode.Append, ...) or GetStream(FileMode.Append, access, sharing) on a CompoundFile StreamInfo.
Common situations: Porting code written for System.IO.File/FileStream (where Append is valid) to the Packaging/CompoundFile API; writing generic file-helper code that passes FileMode.Append.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- SR.FileModeInvalid
- Data space transform stack includes undefined transform…
- FileMode value is not valid.
- Transform identifier type is not supported.
- Transform label name is already in use.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/ac32e39907a7ef20.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs:264
if (parentStorage.Root.OpenAccess == FileAccess.ReadWrite)
{
// Generate the access flags from the access parameter
access = FileAccess.ReadWrite;
}
// Generate the access flags from the access parameter
SafeNativeCompoundFileMethods.UpdateModeFlagFromFileAccess( access, ref grfMode );
// Only SHARE_EXCLUSIVE for now, FileShare issue TBD
grfMode |= SafeNativeCompoundFileConstants.STGM_SHARE_EXCLUSIVE;
CheckAccessMode(grfMode);
// Act based on FileMode
switch(mode)
{
case FileMode.Append:
throw new ArgumentException(
SR.FileModeUnsupported);
case FileMode.Create:
// Check to make sure root container is not read-only, and that
// we're not pointlessly trying to create a read-only stream.
CreateTimeReadOnlyCheck(openFileAccess);
// Close down any existing streams floating out there
if (null != core.exposedStream)
{
((Stream)(core.exposedStream)).Close();
}
core.exposedStream = null;
if( null != core.safeIStream )
{
// Close out existing stream
((IDisposable) core.safeIStream).Dispose();
core.safeIStream = null;View on GitHub (pinned to 81131a70a4)