dotnet/wpf · error · ArgumentException

SR.FileModeUnsupported

Error message

SR.FileModeUnsupported

What it means

StorageRoot.Open rejects FileMode.Append because the compound-file (Structured Storage) API has no append semantics for root storages. The method converts a FileMode into native STGM flags and throws ArgumentException for modes it cannot represent.

Solutions

  1. Use FileMode.OpenOrCreate or FileMode.Open instead of Append.
  2. To add content, open the package with Open/OpenOrCreate and add new parts; compound files do not support append.
  3. Guard FileMode values before calling Package.Open.

Example fix

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

Strategy: validation

Validate before calling

if (mode == FileMode.Append)
    throw new ArgumentException("Append is not supported for compound file packages");

Type guard

bool IsSupportedFileMode(FileMode m) => m == FileMode.Open || m == FileMode.OpenOrCreate || m == FileMode.Create || m == FileMode.CreateNew;

Try / catch

try { pkg = Package.Open(path, mode, access); }
catch (ArgumentException ex) when (ex.Message.Contains("FileMode")) { /* fall back to OpenOrCreate */ }

Prevention

When it happens

Trigger: Calling StorageRoot.Open (or Package.Open on a compound file) with FileMode.Append.

Common situations: Code written generically over FileMode values that handles Append; appending to an existing OPC package by mistake instead of OpenOrCreate.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

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

        int sectorSize )
    {
        int  grfMode = 0;
        int  returnValue = 0;

        // Simple path validation
        ContainerUtilities.CheckStringAgainstNullAndEmpty( path, "Path" );

        Guid IID_IStorage = new Guid(0x0000000B,0x0000,0x0000,0xC0,0x00,
                                     0x00,0x00,0x00,0x00,0x00,0x46);

        IStorage newRootStorage;

        ////////////////////////////////////////////////////////////////////
        // Generate STGM from FileMode
        switch(mode)
        {
            case FileMode.Append:
                throw new ArgumentException(
                    SR.FileModeUnsupported);
            case FileMode.Create:
                grfMode |= SafeNativeCompoundFileConstants.STGM_CREATE;
                break;
            case FileMode.CreateNew:
                {
                    FileInfo existTest = new FileInfo(path);
                    if( existTest.Exists )
                    {
                        throw new IOException(
                            SR.FileAlreadyExists);
                    }
                }
                goto case FileMode.Create;
            case FileMode.Open:
                break;
            case FileMode.OpenOrCreate:
                {

View on GitHub (pinned to 81131a70a4)