{"record":{"id":"ac32e39907a7ef20","repo":"dotnet/wpf","slug":"sr-filemodeunsupported-streaminfo","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/StreamInfo.cs","lineNumber":264,"sourceCode":"            if (parentStorage.Root.OpenAccess == FileAccess.ReadWrite)\n            {\n                // Generate the access flags from the access parameter\n                access = FileAccess.ReadWrite;\n            }\n\n            // Generate the access flags from the access parameter\n            SafeNativeCompoundFileMethods.UpdateModeFlagFromFileAccess( access, ref grfMode );\n\n            // Only SHARE_EXCLUSIVE for now, FileShare issue TBD\n            grfMode |= SafeNativeCompoundFileConstants.STGM_SHARE_EXCLUSIVE;\n\n            CheckAccessMode(grfMode);\n\n            // Act based on FileMode\n            switch(mode)\n            {\n                case FileMode.Append:\n                    throw new ArgumentException(\n                        SR.FileModeUnsupported);\n                case FileMode.Create:\n                    // Check to make sure root container is not read-only, and that\n                    //  we're not pointlessly trying to create a read-only stream.\n                    CreateTimeReadOnlyCheck(openFileAccess);\n                    \n                    // Close down any existing streams floating out there\n                    if (null != core.exposedStream)\n                    {\n                        ((Stream)(core.exposedStream)).Close();\n                    }\n                    core.exposedStream = null;\n                    \n                    if( null != core.safeIStream )\n                    {\n                        // Close out existing stream\n                        ((IDisposable) core.safeIStream).Dispose();\n                        core.safeIStream = null;","sourceCodeStart":246,"sourceCodeEnd":282,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StreamInfo.cs#L246-L282","documentation":"ArgumentException thrown by StreamInfo.GetStream when FileMode.Append is requested. Append mode has no meaning for OLE structured-storage streams inside a compound file, so the library rejects it up front. The caller must pick a FileMode supported by the compound file model (Create, CreateNew, Open, OpenOrCreate).","triggerScenarios":"Calling streamInfo.GetStream(FileMode.Append, ...) or GetStream(FileMode.Append, access, sharing) on a CompoundFile StreamInfo.","commonSituations":"Porting code written for System.IO.File/FileStream (where Append is valid) to the Packaging/CompoundFile API; writing generic file-helper code that passes FileMode.Append.","solutions":["Replace FileMode.Append with FileMode.OpenOrCreate and seek to the end of the stream if append semantics are needed.","Use FileMode.Open with FileAccess.Write, then seek to stream length before writing.","Restructure the call site so append-style writes use a standard FileStream outside the compound file if applicable."],"exampleFix":"// before\nStream s = streamInfo.GetStream(FileMode.Append, FileAccess.Write);\n// after\nStream s = streamInfo.GetStream(FileMode.OpenOrCreate, FileAccess.Write);\ns.Seek(0, SeekOrigin.End); // append semantics","handlingStrategy":"validation","validationCode":"if (mode == FileMode.Append) throw new ArgumentException(\"FileMode.Append is not supported by CompoundFile StreamInfo.GetStream; use OpenOrCreate and seek to end.\");","typeGuard":"bool IsSupportedGetStreamMode(FileMode m) => m == FileMode.Create || m == FileMode.Open || m == FileMode.OpenOrCreate;","tryCatchPattern":"try { s = streamInfo.GetStream(mode, access); }\ncatch (ArgumentException) { s = streamInfo.GetStream(FileMode.OpenOrCreate, access); s.Seek(0, SeekOrigin.End); }","preventionTips":["Whitelist FileModes per API: GetStream supports Create/Open/OpenOrCreate only.","Never pass through user/config-supplied FileModes unvalidated.","Implement append by seeking to end after OpenOrCreate."],"tags":["argument","filemode","packaging","compound-file","unsupported"],"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"}