dotnet/wpf · error · ArgumentException

STG_E_INVALIDFLAG

STG_E_INVALIDFLAG

Error message

SR.StorageFlagsUnsupported

What it means

The native open call returned STG_E_INVALIDFLAG, meaning the STGM flags generated from FileMode/FileAccess/FileShare are not accepted by StgOpenStorageEx. StorageRoot.Open wraps this in an ArgumentException with StorageFlagsUnsupported and an inner COMException.

Solutions

  1. Use conventional combinations: Open+Read+Read/ReadWrite, or OpenOrCreate/Create+ReadWrite.
  2. Check the inner COMException HResult to confirm STG_E_INVALIDFLAG and adjust the mode/access/share trio.
  3. Avoid custom or bit-OR'd FileAccess values when opening packages.
  4. Update to a fixed framework version if triggered by a framework bug.

Example fix

// before
Package.Open(path, FileMode.Open, FileAccess.Write, FileShare.Read);
// after
Package.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
Defensive patterns

Strategy: validation

Validate before calling

// use only well-known mode/access/share triples
var valid = (mode, access) switch {
    (FileMode.Open, FileAccess.Read) => true,
    (FileMode.OpenOrCreate, FileAccess.ReadWrite) => true,
    (FileMode.Create, FileAccess.ReadWrite) => true,
    _ => false
};
if (!valid) throw new ArgumentException("Unsupported mode/access combination");

Try / catch

try { pkg = Package.Open(path, mode, access, share); }
catch (ArgumentException ex) when (ex.InnerException is COMException ce && ce.HResult == unchecked((int)0x80030011)) { /* retry with standard flags */ }

Prevention

When it happens

Trigger: Combining FileMode/Access/Share values that produce an invalid STGM combination (e.g. write access without an appropriate share/mode pairing), or opening a storage with flags it does not support.

Common situations: Requesting FileAccess.Write with FileMode.Open on a file opened read-only elsewhere; unusual access/share pairings mapped onto legacy Structured Storage flags.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/59763301b8fd788b. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs:355

                grfMode,
                stgFormatDocFile,
                0,
                IntPtr.Zero,
                IntPtr.Zero,
                ref IID_IStorage,
                out newRootStorage );
        }

        switch( returnValue )
        {
            case SafeNativeCompoundFileConstants.S_OK:
                return StorageRoot.CreateOnIStorage( 
                    newRootStorage );
            case SafeNativeCompoundFileConstants.STG_E_FILENOTFOUND:
                throw new FileNotFoundException( 
                    SR.ContainerNotFound);
            case SafeNativeCompoundFileConstants.STG_E_INVALIDFLAG:
                throw new ArgumentException( 
                    SR.StorageFlagsUnsupported,
                    new COMException(
                        SR.CFAPIFailure, 
                        returnValue));
            default:
                throw new IOException(
                    SR.ContainerCanNotOpen,
                    new COMException(
                        SR.CFAPIFailure, 
                        returnValue));
        }
    }

    /// <summary>
    /// Clean up this container storage instance
    /// </summary>
    internal void Close()
    {

View on GitHub (pinned to 81131a70a4)