dotnet/wpf · error · ArgumentException

SR.FileModeInvalid

Error message

SR.FileModeInvalid

What it means

ArgumentException thrown by StreamInfo.GetStream for any FileMode value outside the switch's known cases (the default branch). Only Append/Create/CreateNew/Open/OpenOrCreate/Truncate are enum members, and most are individually rejected; this catch-all also guards against invalid casts like (FileMode)99. Effectively the caller passed a FileMode the compound-file stream API cannot honor or an out-of-range enum value.

Solutions

  1. Use only FileMode values valid for this API: Create, Open, or OpenOrCreate.
  2. Validate the FileMode with Enum.IsDefined before passing it in.
  3. Fix data sources (config/serialization) that produce invalid enum values.

Example fix

// before
var mode = (FileMode)configValue; // may be invalid
Stream s = streamInfo.GetStream(mode, FileAccess.Read);
// after
var mode = (FileMode)configValue;
if (!Enum.IsDefined(typeof(FileMode), mode) || mode == FileMode.Append || mode == FileMode.Truncate)
    throw new ArgumentException("Unsupported FileMode for compound file streams.");
Stream s = streamInfo.GetStream(mode, FileAccess.Read);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(FileMode), mode)) throw new ArgumentException($"Invalid FileMode value: {mode}");

Type guard

bool IsValidFileMode(object v) => v is FileMode m && Enum.IsDefined(typeof(FileMode), m);

Try / catch

try { s = streamInfo.GetStream(mode, access); }
catch (ArgumentException) { s = streamInfo.GetStream(FileMode.OpenOrCreate, access); }

Prevention

When it happens

Trigger: Calling streamInfo.GetStream with FileMode.Append, FileMode.Truncate, or an arbitrary out-of-range value cast to FileMode; passing an uninitialized/garbage FileMode from deserialized or computed data.

Common situations: Enum values read from config or wire data without validation; generic wrapper methods that forward user-supplied FileModes; code paths after enum changes.

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


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs:364

                        // else - proceed with open
                    }

                    if( null == openedIStream )
                    {
                        // If we make it here, it means the create stream call failed
                        //  because of a STG_E_FILEALREADYEXISTS 
                        //  or container is read-only
                        openedIStream = OpenStreamOnParentIStorage(
                                core.streamName, 
                            grfMode );
                    }
                    break;
                case FileMode.Truncate:
                    throw new ArgumentException(
                        SR.FileModeUnsupported);
                default:
                    throw new ArgumentException(
                        SR.FileModeInvalid);
            }

            core.safeIStream = openedIStream;

            Stream returnStream = 
                BuildStreamOnUnderlyingIStream( core.safeIStream, openFileAccess, this );

            core.exposedStream = returnStream;

            return returnStream;
        }

        /***********************************************************************/
        // Internal/Private functionality

        /// <summary>
        /// Creates a stream with all default parameters

View on GitHub (pinned to 81131a70a4)