{"record":{"id":"b4a255c43524a616","repo":"dotnet/wpf","slug":"write-only-mode-is-not-supported","errorCode":null,"errorMessage":"Write-only mode is not supported.","messagePattern":"Write-only mode is not supported\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/NativeCompoundFileAPIs.cs","lineNumber":31,"sourceCode":"using CultureInfo = System.Globalization.CultureInfo;\n\nnamespace MS.Internal.IO.Packaging.CompoundFile\n{\n    internal static class SafeNativeCompoundFileMethods\n    {\n        /// <summary>\n        /// Utility function to update a grfMode value based on FileAccess.\n        /// 6/12/2002: Fixes bug #4938, 4960, 5096, 4858\n        /// </summary>\n        /// <param name=\"access\">FileAccess we're translating</param>\n        /// <param name=\"grfMode\">Mode flag parameter to modify</param>\n        internal static void UpdateModeFlagFromFileAccess( FileAccess access, ref int grfMode )\n        {\n            // Supporting write-only scenarios container-wide gets tricky and it \n            //  is rarely used.  Don't support it for now because of poor \n            //  cost/benefit ratio.\n            if( FileAccess.Write == access )\n                throw new NotSupportedException(\n                    SR.WriteOnlyUnsupported);\n            \n            // Generate STGM from FileAccess\n            // STGM_READ is 0x00, so it's \"by default\"\n            if( (  FileAccess.ReadWrite                == (access &  FileAccess.ReadWrite) )  ||\n                ( (FileAccess.Read | FileAccess.Write) == (access & (FileAccess.Read | FileAccess.Write))) )\n            {\n                grfMode |= SafeNativeCompoundFileConstants.STGM_READWRITE;\n            }\n            else if( FileAccess.Write == (access & FileAccess.Write) )\n            {\n                grfMode |= SafeNativeCompoundFileConstants.STGM_WRITE;\n            }\n            else if( FileAccess.Read != (access & FileAccess.Read))\n            {\n                throw new ArgumentException(\n                    SR.FileAccessInvalid);\n            }","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/NativeCompoundFileAPIs.cs#L13-L49","documentation":"UpdateModeFlagFromFileAccess converts a FileAccess into an OLE STGM mode. Write-only access (FileAccess.Write alone) is deliberately unsupported because write-only compound-file scenarios are tricky and rarely used, so a NotSupportedException (SR.WriteOnlyUnsupported) is thrown.","triggerScenarios":"Opening a compound file (e.g. via CompoundFileContainer or related packaging APIs) with FileAccess.Write but not FileAccess.Read.","commonSituations":"Code that opens files for writing-only elsewhere and reuses FileAccess.Write when opening a WPF package container.","solutions":["Open with FileAccess.ReadWrite instead of FileAccess.Write","If write-only semantics are desired, emulate by opening ReadWrite and not reading","Refactor to avoid reusing the write-only FileAccess value for packaging APIs"],"exampleFix":"// before\ncontainer = new CompoundFileContainer(stream, FileAccess.Write, ...);\n// after\ncontainer = new CompoundFileContainer(stream, FileAccess.ReadWrite, ...);","handlingStrategy":"try-catch","validationCode":"if (access == FileAccess.Write) access = FileAccess.ReadWrite; // write-only unsupported","typeGuard":"static bool IsSupportedAccess(FileAccess a) => a != FileAccess.Write;","tryCatchPattern":"try { OpenContainer(stream, access); } catch (NotSupportedException) { access = FileAccess.ReadWrite; OpenContainer(stream, access); }","preventionTips":["Never open WPF packaging containers with FileAccess.Write alone","Default to FileAccess.ReadWrite for compound-file scenarios","Centralize FileAccess selection in one helper"],"tags":["wpf","packaging","notsupported"],"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"}