{"record":{"id":"2ff2bf8f81eb7e70","repo":"dotnet/wpf","slug":"sr-filemodeinvalid-streaminfo","errorCode":null,"errorMessage":"SR.FileModeInvalid","messagePattern":"SR\\.FileModeInvalid","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs","lineNumber":364,"sourceCode":"\n                        // else - proceed with open\n                    }\n\n                    if( null == openedIStream )\n                    {\n                        // If we make it here, it means the create stream call failed\n                        //  because of a STG_E_FILEALREADYEXISTS \n                        //  or container is read-only\n                        openedIStream = OpenStreamOnParentIStorage(\n                                core.streamName, \n                            grfMode );\n                    }\n                    break;\n                case FileMode.Truncate:\n                    throw new ArgumentException(\n                        SR.FileModeUnsupported);\n                default:\n                    throw new ArgumentException(\n                        SR.FileModeInvalid);\n            }\n\n            core.safeIStream = openedIStream;\n\n            Stream returnStream = \n                BuildStreamOnUnderlyingIStream( core.safeIStream, openFileAccess, this );\n\n            core.exposedStream = returnStream;\n\n            return returnStream;\n        }\n\n        /***********************************************************************/\n        // Internal/Private functionality\n\n        /// <summary>\n        /// Creates a stream with all default parameters","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs#L346-L382","documentation":"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.","triggerScenarios":"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.","commonSituations":"Enum values read from config or wire data without validation; generic wrapper methods that forward user-supplied FileModes; code paths after enum changes.","solutions":["Use only FileMode values valid for this API: Create, Open, or OpenOrCreate.","Validate the FileMode with Enum.IsDefined before passing it in.","Fix data sources (config/serialization) that produce invalid enum values."],"exampleFix":"// before\nvar mode = (FileMode)configValue; // may be invalid\nStream s = streamInfo.GetStream(mode, FileAccess.Read);\n// after\nvar mode = (FileMode)configValue;\nif (!Enum.IsDefined(typeof(FileMode), mode) || mode == FileMode.Append || mode == FileMode.Truncate)\n    throw new ArgumentException(\"Unsupported FileMode for compound file streams.\");\nStream s = streamInfo.GetStream(mode, FileAccess.Read);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(FileMode), mode)) throw new ArgumentException($\"Invalid FileMode value: {mode}\");","typeGuard":"bool IsValidFileMode(object v) => v is FileMode m && Enum.IsDefined(typeof(FileMode), m);","tryCatchPattern":"try { s = streamInfo.GetStream(mode, access); }\ncatch (ArgumentException) { s = streamInfo.GetStream(FileMode.OpenOrCreate, access); }","preventionTips":["Always validate enum values from config, wire, or deserialization with Enum.IsDefined.","Use only Open/Create/OpenOrCreate for compound-file streams.","Avoid unchecked casts to FileMode."],"tags":["argument","filemode","enum","packaging","compound-file"],"backgroundTag":"invalid-enum-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}