{"record":{"id":"ee6d60f105e334c1","repo":"dotnet/wpf","slug":"sr-filemodeinvalid","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/StorageRoot.cs","lineNumber":280,"sourceCode":"            case FileMode.OpenOrCreate:\n                {\n                    FileInfo existTest = new FileInfo(path);\n                    if( existTest.Exists )\n                    {\n                        // File exists, use open code path\n                        goto case FileMode.Open;\n                    }\n                    else\n                    {\n                        // File does not exist, use create code path\n                        goto case FileMode.Create;\n                    }\n                }\n            case FileMode.Truncate:\n                throw new ArgumentException(\n                    SR.FileModeUnsupported);\n            default:\n                throw new ArgumentException(\n                    SR.FileModeInvalid);\n        }\n\n        // Generate the access flags from the access parameter\n        SafeNativeCompoundFileMethods.UpdateModeFlagFromFileAccess( access, ref grfMode );\n\n        // Generate STGM from FileShare\n\n        // Note: the .NET SDK does not specify the proper behavior in reaction to\n        //  incompatible flags being sent in together.  Should ArgumentException be\n        //  thrown?  Or do some values \"trump\" others?\n        if( 0 != (share & FileShare.Inheritable) )\n        {\n            throw new ArgumentException(\n                SR.FileShareUnsupported);\n        }\n        else if( share == FileShare.None ) // FileShare.None is zero, using \"&\" to check causes unreachable code error\n        {","sourceCodeStart":262,"sourceCodeEnd":298,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs#L262-L298","documentation":"StorageRoot.Open received a FileMode value outside the set it understands (Create, CreateNew, Open, OpenOrCreate, Truncate, Append), e.g. an undefined or invalid enum cast. It throws ArgumentException with FileModeInvalid from the default branch.","triggerScenarios":"Calling StorageRoot.Open / Package.Open with a FileMode value not defined by System.IO.FileMode, typically from an invalid cast of an int or deserialized enum.","commonSituations":"Parsing mode strings from config into FileMode without validation; (FileMode)99 style casts; serialization round-trip corruption.","solutions":["Validate the FileMode value with Enum.IsDefined before passing it.","Fix the source of the invalid cast/deserialization.","Default to FileMode.OpenOrCreate when the value is unknown."],"exampleFix":"// before\nvar mode = (FileMode)intFromConfig; // may be undefined\nPackage.Open(path, mode, access);\n// after\nvar mode = Enum.IsDefined(typeof(FileMode), intFromConfig) ? (FileMode)intFromConfig : FileMode.OpenOrCreate;\nPackage.Open(path, mode, access);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(FileMode), mode))\n    throw new InvalidDataException($\"Undefined FileMode value: {mode}\");","typeGuard":"bool IsDefinedFileMode(FileMode m) => Enum.IsDefined(typeof(FileMode), m);","tryCatchPattern":"try { pkg = Package.Open(path, mode, access); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"invalid\")) { /* fix enum source */ }","preventionTips":["Validate enums parsed from config/input with Enum.IsDefined.","Avoid raw int-to-enum casts for FileMode."],"tags":["io","filemode","enum","argument"],"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-22T01:17:13.364Z"}