{"record":{"id":"feb8af7427e0284e","repo":"dotnet/wpf","slug":"mode-word-contains-flags-that-are-not-valid","errorCode":null,"errorMessage":"Mode word contains flags that are not valid.","messagePattern":"Mode word contains flags that are not valid\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/XpsFilter.cs","lineNumber":366,"sourceCode":"        /// </param>\n        /// <param name=\"dwMode\">The mode in which to open pszFileName. </param>\n        void IPersistFile.Load(string pszFileName, int dwMode)\n        {\n            FileMode fileMode;\n            FileAccess fileAccess;\n            FileShare fileSharing;\n\n            // Check argument.\n            if (pszFileName == null || pszFileName == String.Empty)\n            {\n                throw new ArgumentException(SR.FileNameNullOrEmpty, nameof(pszFileName));\n            }\n\n            // Convert mode information in flag.\n            switch ((STGM_FLAGS)(dwMode & (int)STGM_FLAGS.MODE))\n            {\n                case STGM_FLAGS.CREATE:\n                    throw new ArgumentException(SR.FilterLoadInvalidModeFlag, nameof(dwMode));\n\n                default:\n                    fileMode = FileMode.Open;\n                    break;\n            }\n\n            // Convert access flag.\n            switch ((STGM_FLAGS)(dwMode & (int)STGM_FLAGS.ACCESS))\n            {\n                case STGM_FLAGS.READ:\n                case STGM_FLAGS.READWRITE:\n                    fileAccess = FileAccess.Read;\n                    break;\n\n                default:\n                    throw new ArgumentException(SR.FilterLoadInvalidModeFlag, nameof(dwMode));\n            }\n","sourceCodeStart":348,"sourceCodeEnd":384,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/XpsFilter.cs#L348-L384","documentation":"XpsFilter.Load converts the STGM mode word into a FileMode. The MODE portion of dwMode is switched on, and only READ/READWRITE are meaningful; CREATE is explicitly rejected with ArgumentException (SR.FilterLoadInvalidModeFlag). A filter is a read-only consumer, so create/truncate-style modes are invalid.","triggerScenarios":"Calling IPersistFile.Load with dwMode whose STGM MODE bits equal STGM_CREATE (or otherwise land in the CREATE case of the switch).","commonSituations":"Reusing persistence code written for writable storage to drive the filter; passing STGM_CREATE|STGM_READ out of habit.","solutions":["Pass STGM_READ or STGM_READWRITE in the mode word when calling Load","Remove STGM_CREATE (and WRITE/READWRITE+CREATE combinations) from dwMode","Use FileAccess.Read-equivalent semantics; filters never create files"],"exampleFix":"// before\nfilter.Load(path, (int)(STGM_FLAGS.CREATE | STGM_FLAGS.READ));\n// after\nfilter.Load(path, (int)STGM_FLAGS.READ);","handlingStrategy":"validation","validationCode":"var mode = (STGM_FLAGS)(dwMode & (int)STGM_FLAGS.MODE);\nbool ok = mode == STGM_FLAGS.READ || mode == STGM_FLAGS.READWRITE;","typeGuard":null,"tryCatchPattern":"try { filter.Load(path, dwMode); }\ncatch (ArgumentException ex) { /* bad STGM mode; fix flags */ }","preventionTips":["Always pass STGM_READ (or READWRITE) to filters — never CREATE","Keep a single STGM_FLAGS mapping constant shared by callers","Review P/Invoke signatures that marshal dwMode"],"tags":["argument","flags","csharp","wpf"],"backgroundTag":"invalid-flag-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"}