{"record":{"id":"4cd121584fb96698","repo":"dotnet/wpf","slug":"sr-filemodeunsupported","errorCode":null,"errorMessage":"SR.FileModeUnsupported","messagePattern":"SR\\.FileModeUnsupported","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs","lineNumber":245,"sourceCode":"        int sectorSize )\n    {\n        int  grfMode = 0;\n        int  returnValue = 0;\n\n        // Simple path validation\n        ContainerUtilities.CheckStringAgainstNullAndEmpty( path, \"Path\" );\n\n        Guid IID_IStorage = new Guid(0x0000000B,0x0000,0x0000,0xC0,0x00,\n                                     0x00,0x00,0x00,0x00,0x00,0x46);\n\n        IStorage newRootStorage;\n\n        ////////////////////////////////////////////////////////////////////\n        // Generate STGM from FileMode\n        switch(mode)\n        {\n            case FileMode.Append:\n                throw new ArgumentException(\n                    SR.FileModeUnsupported);\n            case FileMode.Create:\n                grfMode |= SafeNativeCompoundFileConstants.STGM_CREATE;\n                break;\n            case FileMode.CreateNew:\n                {\n                    FileInfo existTest = new FileInfo(path);\n                    if( existTest.Exists )\n                    {\n                        throw new IOException(\n                            SR.FileAlreadyExists);\n                    }\n                }\n                goto case FileMode.Create;\n            case FileMode.Open:\n                break;\n            case FileMode.OpenOrCreate:\n                {","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs#L227-L263","documentation":"StorageRoot.Open rejects FileMode.Append because the compound-file (Structured Storage) API has no append semantics for root storages. The method converts a FileMode into native STGM flags and throws ArgumentException for modes it cannot represent.","triggerScenarios":"Calling StorageRoot.Open (or Package.Open on a compound file) with FileMode.Append.","commonSituations":"Code written generically over FileMode values that handles Append; appending to an existing OPC package by mistake instead of OpenOrCreate.","solutions":["Use FileMode.OpenOrCreate or FileMode.Open instead of Append.","To add content, open the package with Open/OpenOrCreate and add new parts; compound files do not support append.","Guard FileMode values before calling Package.Open."],"exampleFix":"// before\nPackage.Open(path, FileMode.Append, FileAccess.Write);\n// after\nPackage.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);","handlingStrategy":"validation","validationCode":"if (mode == FileMode.Append)\n    throw new ArgumentException(\"Append is not supported for compound file packages\");","typeGuard":"bool IsSupportedFileMode(FileMode m) => m == FileMode.Open || m == FileMode.OpenOrCreate || m == FileMode.Create || m == FileMode.CreateNew;","tryCatchPattern":"try { pkg = Package.Open(path, mode, access); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"FileMode\")) { /* fall back to OpenOrCreate */ }","preventionTips":["Never use FileMode.Append with Package.Open / StorageRoot.Open.","Centralize FileMode-to-package mapping in one validated helper.","Remember compound files have no append semantics."],"tags":["io","filemode","argument"],"backgroundTag":"unsupported-operation","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"}