{"record":{"id":"9bc458ae6b7f68a8","repo":"dotnet/wpf","slug":"fileaccess-value-is-not-valid","errorCode":null,"errorMessage":"FileAccess value is not valid.","messagePattern":"FileAccess value is not valid\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/NativeCompoundFileAPIs.cs","lineNumber":47,"sourceCode":"            //  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            }\n        }\n\n        internal static int SafeStgCreateDocfileOnStream(\n            Stream s,\n            int grfMode,\n            out IStorage ppstgOpen\n            )\n        {\n\n            Invariant.Assert(s != null, \"s cannot be null\");\n\n            UnsafeNativeCompoundFileMethods.UnsafeNativeIStorage storage;\n            UnsafeNativeCompoundFileMethods.UnsafeLockBytesOnStream lockByteStream = new UnsafeNativeCompoundFileMethods.UnsafeLockBytesOnStream(s);\n            int result;\n\n            result = UnsafeNativeCompoundFileMethods.StgCreateDocfileOnILockBytes(","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/NativeCompoundFileAPIs.cs#L29-L65","documentation":"After checking for ReadWrite and Write access, UpdateModeFlagFromFileAccess throws ArgumentException (SR.FileAccessInvalid) if the FileAccess value does not include the Read bit, i.e. it is not a recognized FileAccess combination (0, or an out-of-range value).","triggerScenarios":"Calling a compound-file open/create API with a FileAccess value of 0 (FileAccess.None) or an invalid casted integer.","commonSituations":"FileAccess field defaulted to 0, uninitialized variable, or an invalid value deserialized from config/state.","solutions":["Pass FileAccess.Read, FileAccess.Write, or FileAccess.ReadWrite","Initialize the FileAccess variable properly before use","Validate the access value from config/deserialization before passing it"],"exampleFix":"// before\nFileAccess access = 0;\nOpenCompoundFile(stream, access);\n// after\nFileAccess access = FileAccess.Read;\nOpenCompoundFile(stream, access);","handlingStrategy":"type-guard","validationCode":"if (access != FileAccess.Read && access != FileAccess.Write && access != FileAccess.ReadWrite) throw new ArgumentException(\"Invalid FileAccess\", nameof(access));","typeGuard":"static bool IsValidFileAccess(FileAccess a) => a == FileAccess.Read || a == FileAccess.Write || a == FileAccess.ReadWrite;","tryCatchPattern":"try { OpenContainer(stream, access); } catch (ArgumentException ex) when (ex.ParamName == null && ex.Message.Contains(\"FileAccess\")) { /* fix access value */ }","preventionTips":["Initialize FileAccess variables to a valid member, never 0/default","Validate FileAccess values deserialized from config","Use the enum's defined values only; avoid casting raw ints"],"tags":["wpf","packaging","fileaccess","invalid-enum"],"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"}