dotnet/wpf · error · ArgumentException
FileAccess value is not valid.
Error message
FileAccess value is not valid.
What it means
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).
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
Example fix
// before FileAccess access = 0; OpenCompoundFile(stream, access); // after FileAccess access = FileAccess.Read; OpenCompoundFile(stream, access);
Defensive patterns
Strategy: type-guard
Validate before calling
if (access != FileAccess.Read && access != FileAccess.Write && access != FileAccess.ReadWrite) throw new ArgumentException("Invalid FileAccess", nameof(access)); Type guard
static bool IsValidFileAccess(FileAccess a) => a == FileAccess.Read || a == FileAccess.Write || a == FileAccess.ReadWrite;
Try / catch
try { OpenContainer(stream, access); } catch (ArgumentException ex) when (ex.ParamName == null && ex.Message.Contains("FileAccess")) { /* fix access value */ } Prevention
- 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
When it happens
Trigger: Calling a compound-file open/create API with a FileAccess value of 0 (FileAccess.None) or an invalid casted integer.
Common situations: FileAccess field defaulted to 0, uninitialized variable, or an invalid value deserialized from config/state.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- ' ' cannot contain the path delimiter: ' '.
- ' ' cannot start with the reserved character range…
- ' ' ID is not a valid XSD ID.
- ' ' is not a valid value for ' '.
- Can only countersign parts with Digital Signature…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/9bc458ae6b7f68a8.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/NativeCompoundFileAPIs.cs:47
// cost/benefit ratio.
if( FileAccess.Write == access )
throw new NotSupportedException(
SR.WriteOnlyUnsupported);
// Generate STGM from FileAccess
// STGM_READ is 0x00, so it's "by default"
if( ( FileAccess.ReadWrite == (access & FileAccess.ReadWrite) ) ||
( (FileAccess.Read | FileAccess.Write) == (access & (FileAccess.Read | FileAccess.Write))) )
{
grfMode |= SafeNativeCompoundFileConstants.STGM_READWRITE;
}
else if( FileAccess.Write == (access & FileAccess.Write) )
{
grfMode |= SafeNativeCompoundFileConstants.STGM_WRITE;
}
else if( FileAccess.Read != (access & FileAccess.Read))
{
throw new ArgumentException(
SR.FileAccessInvalid);
}
}
internal static int SafeStgCreateDocfileOnStream(
Stream s,
int grfMode,
out IStorage ppstgOpen
)
{
Invariant.Assert(s != null, "s cannot be null");
UnsafeNativeCompoundFileMethods.UnsafeNativeIStorage storage;
UnsafeNativeCompoundFileMethods.UnsafeLockBytesOnStream lockByteStream = new UnsafeNativeCompoundFileMethods.UnsafeLockBytesOnStream(s);
int result;
result = UnsafeNativeCompoundFileMethods.StgCreateDocfileOnILockBytes(View on GitHub (pinned to 81131a70a4)