{"record":{"id":"b5c6b28318e666d9","repo":"dotnet/wpf","slug":"sr-createmodemustbecreateoropen","errorCode":null,"errorMessage":"SR.CreateModeMustBeCreateOrOpen","messagePattern":"SR\\.CreateModeMustBeCreateOrOpen","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs","lineNumber":149,"sourceCode":"        }\n\n        if( FileMode.Create == mode )\n        {\n            returnValue = SafeNativeCompoundFileMethods.SafeStgCreateDocfileOnStream(\n                baseStream,\n                openFlags | SafeNativeCompoundFileConstants.STGM_CREATE,\n                out storageOnStream);\n        }\n        else if( FileMode.Open == mode )\n        {\n            returnValue = SafeNativeCompoundFileMethods.SafeStgOpenStorageOnStream(\n                baseStream,\n                openFlags,\n                out storageOnStream );\n        }\n        else\n        {\n            throw new ArgumentException(\n                SR.CreateModeMustBeCreateOrOpen);\n        }\n        \n        switch( (uint) returnValue )\n        {\n            case SafeNativeCompoundFileConstants.S_OK:\n                return StorageRoot.CreateOnIStorage( \n                    storageOnStream );\n\n            default:\n                throw new IOException(\n                    SR.UnableToCreateOnStream,\n                    new COMException(\n                        SR.CFAPIFailure, \n                        returnValue));\n        }\n    }\n","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs#L131-L167","documentation":"StorageRoot.CreateOnStream only supports FileMode.Create and FileMode.Open; any other FileMode (CreateNew, OpenOrCreate, Truncate, Append) reaches the else branch and throws ArgumentException with SR.CreateModeMustBeCreateOrOpen. The COM StgCreateDocfile/StgOpenStorage mapping is only defined for these two modes.","triggerScenarios":"Calling StorageRoot.CreateOnStream with FileMode.CreateNew, OpenOrCreate, Truncate, or Append — e.g. passing FileMode.OpenOrCreate because that is habitual for FileStream usage.","commonSituations":"Refactoring code that used FileStream constructors with OpenOrCreate; generic stream-opening helpers that forward arbitrary FileMode values into the compound-file layer.","solutions":["Pass FileMode.Open to open an existing container or FileMode.Create to create/overwrite one.","Map other modes before the call: OpenOrCreate -> check existence then Open or Create; CreateNew -> check existence, then Create; Truncate -> Create on a truncated stream.","Reject unsupported modes early in your own API with a clear message.","See docs: StorageRoot on-stream creation accepts only Create and Open."],"exampleFix":"// before\nvar root = StorageRoot.CreateOnStream(stream, FileMode.OpenOrCreate, FileAccess.ReadWrite); // ArgumentException\n// after\nvar mode = stream.CanRead && stream.Length > 0 ? FileMode.Open : FileMode.Create;\nvar root = StorageRoot.CreateOnStream(stream, mode, FileAccess.ReadWrite);","handlingStrategy":"validation","validationCode":"private static readonly HashSet<FileMode> Allowed = new() { FileMode.Create, FileMode.Open };\nif (!Allowed.Contains(mode))\n    throw new ArgumentException(\"Only FileMode.Create and FileMode.Open are supported\", nameof(mode));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Normalize FileMode before calling: map OpenOrCreate -> Open/Create after an existence check.","Do not forward FileStream-style modes directly into compound-file APIs.","Document that StorageRoot supports only Create and Open."],"tags":["io","compound-file","filemode","argument-error"],"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"}