{"record":{"id":"df477fe064e320b3","repo":"dotnet/wpf","slug":"filemode-value-is-not-valid","errorCode":null,"errorMessage":"FileMode value is not valid.","messagePattern":"FileMode value is not valid\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs","lineNumber":512,"sourceCode":"                        grfMode );    \n                    break;\n                case FileMode.CreateNew:\n                    // If we've created a CFStream, this fails because stream is already there.\n                    if( null != core.safeIStream )\n                        throw new IOException(\n                            SR.StreamAlreadyExist);\n\n                    // Need to call Create API with NULL create flags\n                    createdSafeIStream = CreateStreamOnParentIStorage(\n                            core.streamName, \n                        grfMode );\n                    break;\n                case FileMode.Append:   // None of these are valid in a Create\n                case FileMode.Open:\n                case FileMode.OpenOrCreate:\n                case FileMode.Truncate:\n                default:\n                    throw new ArgumentException(\n                        SR.FileModeInvalid);\n            }\n\n            core.safeIStream = createdSafeIStream;\n            // At this point we passed all previous checks and got the underlying IStream.\n            //  Set our data space label to the given label, and the stream to the retrieved stream.\n            core.dataSpaceLabel = dataSpace;\n            if( null != dataSpace )\n            {\n                dataSpaceManager.CreateDataSpaceMapping( \n                    new CompoundFileStreamReference( parentStorage.FullNameInternal, core.streamName ), \n                    core.dataSpaceLabel );\n            }\n\n            Stream returnStream = \n                BuildStreamOnUnderlyingIStream( core.safeIStream, openFileAccess, this );\n\n            _needToGetTransformInfo = false;    // We created stream with the given dataspace setting","sourceCodeStart":494,"sourceCodeEnd":530,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs#L494-L530","documentation":"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.'","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nstreamInfo.Create(content, FileAccess.Write, FileMode.OpenOrCreate);\n// after\nFileMode mode = /* ... */;\nif (mode != FileMode.Create && mode != FileMode.CreateNew)\n    throw new ArgumentException(\"StreamInfo.Create only accepts Create or CreateNew.\");\nstreamInfo.Create(content, FileAccess.Write, mode);","handlingStrategy":"validation","validationCode":"if (mode != FileMode.Create && mode != FileMode.CreateNew) throw new ArgumentException(\"StreamInfo.Create accepts only FileMode.Create or FileMode.CreateNew.\");","typeGuard":"bool IsValidCreateMode(FileMode m) => m == FileMode.Create || m == FileMode.CreateNew;","tryCatchPattern":"try { streamInfo.Create(content, access, mode); }\ncatch (ArgumentException) { streamInfo.Create(content, access, FileMode.Create); }","preventionTips":["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."],"tags":["argument","filemode","enum","validation","packaging"],"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"}