{"record":{"id":"59763301b8fd788b","repo":"dotnet/wpf","slug":"stg-e-invalidflag","errorCode":"STG_E_INVALIDFLAG","errorMessage":"SR.StorageFlagsUnsupported","messagePattern":"SR\\.StorageFlagsUnsupported","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs","lineNumber":355,"sourceCode":"                grfMode,\n                stgFormatDocFile,\n                0,\n                IntPtr.Zero,\n                IntPtr.Zero,\n                ref IID_IStorage,\n                out newRootStorage );\n        }\n\n        switch( returnValue )\n        {\n            case SafeNativeCompoundFileConstants.S_OK:\n                return StorageRoot.CreateOnIStorage( \n                    newRootStorage );\n            case SafeNativeCompoundFileConstants.STG_E_FILENOTFOUND:\n                throw new FileNotFoundException( \n                    SR.ContainerNotFound);\n            case SafeNativeCompoundFileConstants.STG_E_INVALIDFLAG:\n                throw new ArgumentException( \n                    SR.StorageFlagsUnsupported,\n                    new COMException(\n                        SR.CFAPIFailure, \n                        returnValue));\n            default:\n                throw new IOException(\n                    SR.ContainerCanNotOpen,\n                    new COMException(\n                        SR.CFAPIFailure, \n                        returnValue));\n        }\n    }\n\n    /// <summary>\n    /// Clean up this container storage instance\n    /// </summary>\n    internal void Close()\n    {","sourceCodeStart":337,"sourceCodeEnd":373,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs#L337-L373","documentation":"The native open call returned STG_E_INVALIDFLAG, meaning the STGM flags generated from FileMode/FileAccess/FileShare are not accepted by StgOpenStorageEx. StorageRoot.Open wraps this in an ArgumentException with StorageFlagsUnsupported and an inner COMException.","triggerScenarios":"Combining FileMode/Access/Share values that produce an invalid STGM combination (e.g. write access without an appropriate share/mode pairing), or opening a storage with flags it does not support.","commonSituations":"Requesting FileAccess.Write with FileMode.Open on a file opened read-only elsewhere; unusual access/share pairings mapped onto legacy Structured Storage flags.","solutions":["Use conventional combinations: Open+Read+Read/ReadWrite, or OpenOrCreate/Create+ReadWrite.","Check the inner COMException HResult to confirm STG_E_INVALIDFLAG and adjust the mode/access/share trio.","Avoid custom or bit-OR'd FileAccess values when opening packages.","Update to a fixed framework version if triggered by a framework bug."],"exampleFix":"// before\nPackage.Open(path, FileMode.Open, FileAccess.Write, FileShare.Read);\n// after\nPackage.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);","handlingStrategy":"validation","validationCode":"// use only well-known mode/access/share triples\nvar valid = (mode, access) switch {\n    (FileMode.Open, FileAccess.Read) => true,\n    (FileMode.OpenOrCreate, FileAccess.ReadWrite) => true,\n    (FileMode.Create, FileAccess.ReadWrite) => true,\n    _ => false\n};\nif (!valid) throw new ArgumentException(\"Unsupported mode/access combination\");","typeGuard":null,"tryCatchPattern":"try { pkg = Package.Open(path, mode, access, share); }\ncatch (ArgumentException ex) when (ex.InnerException is COMException ce && ce.HResult == unchecked((int)0x80030011)) { /* retry with standard flags */ }","preventionTips":["Stick to conventional FileMode/FileAccess/FileShare combinations.","Read the inner COMException HResult to diagnose flag problems."],"tags":["io","flags","compound-file"],"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"}