dotnet/wpf · error · ArgumentException
Mode word contains flags that are not valid.
Error message
Mode word contains flags that are not valid.
What it means
XpsFilter.Load converts the STGM mode word into a FileMode. The MODE portion of dwMode is switched on, and only READ/READWRITE are meaningful; CREATE is explicitly rejected with ArgumentException (SR.FilterLoadInvalidModeFlag). A filter is a read-only consumer, so create/truncate-style modes are invalid.
Solutions
- Pass STGM_READ or STGM_READWRITE in the mode word when calling Load
- Remove STGM_CREATE (and WRITE/READWRITE+CREATE combinations) from dwMode
- Use FileAccess.Read-equivalent semantics; filters never create files
Example fix
// before filter.Load(path, (int)(STGM_FLAGS.CREATE | STGM_FLAGS.READ)); // after filter.Load(path, (int)STGM_FLAGS.READ);
Defensive patterns
Strategy: validation
Validate before calling
var mode = (STGM_FLAGS)(dwMode & (int)STGM_FLAGS.MODE); bool ok = mode == STGM_FLAGS.READ || mode == STGM_FLAGS.READWRITE;
Try / catch
try { filter.Load(path, dwMode); }
catch (ArgumentException ex) { /* bad STGM mode; fix flags */ } Prevention
- Always pass STGM_READ (or READWRITE) to filters — never CREATE
- Keep a single STGM_FLAGS mapping constant shared by callers
- Review P/Invoke signatures that marshal dwMode
When it happens
Trigger: Calling IPersistFile.Load with dwMode whose STGM MODE bits equal STGM_CREATE (or otherwise land in the CREATE case of the switch).
Common situations: Reusing persistence code written for writable storage to drive the filter; passing STGM_CREATE|STGM_READ out of habit.
Understand the failure class
Background: "unknown output mode", "invalid value for flag", "expects true/false": fixing invalid flag value errors in CLI tools — this error's family across 24 libraries.
Related errors
- File name is null or empty.
- " }} " element found. Expected fixed page element ( }} ).
- ' ' ContentType is not valid.
- ' ' ID is not a valid XSD ID.
- ArgumentNullException
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/feb8af7427e0284e.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/XpsFilter.cs:366
/// </param>
/// <param name="dwMode">The mode in which to open pszFileName. </param>
void IPersistFile.Load(string pszFileName, int dwMode)
{
FileMode fileMode;
FileAccess fileAccess;
FileShare fileSharing;
// Check argument.
if (pszFileName == null || pszFileName == String.Empty)
{
throw new ArgumentException(SR.FileNameNullOrEmpty, nameof(pszFileName));
}
// Convert mode information in flag.
switch ((STGM_FLAGS)(dwMode & (int)STGM_FLAGS.MODE))
{
case STGM_FLAGS.CREATE:
throw new ArgumentException(SR.FilterLoadInvalidModeFlag, nameof(dwMode));
default:
fileMode = FileMode.Open;
break;
}
// Convert access flag.
switch ((STGM_FLAGS)(dwMode & (int)STGM_FLAGS.ACCESS))
{
case STGM_FLAGS.READ:
case STGM_FLAGS.READWRITE:
fileAccess = FileAccess.Read;
break;
default:
throw new ArgumentException(SR.FilterLoadInvalidModeFlag, nameof(dwMode));
}
View on GitHub (pinned to 81131a70a4)