dotnet/wpf · error · ArgumentException

SR.FileModeInvalid

Error message

SR.FileModeInvalid

What it means

StorageRoot.Open received a FileMode value outside the set it understands (Create, CreateNew, Open, OpenOrCreate, Truncate, Append), e.g. an undefined or invalid enum cast. It throws ArgumentException with FileModeInvalid from the default branch.

Solutions

  1. Validate the FileMode value with Enum.IsDefined before passing it.
  2. Fix the source of the invalid cast/deserialization.
  3. Default to FileMode.OpenOrCreate when the value is unknown.

Example fix

// before
var mode = (FileMode)intFromConfig; // may be undefined
Package.Open(path, mode, access);
// after
var mode = Enum.IsDefined(typeof(FileMode), intFromConfig) ? (FileMode)intFromConfig : FileMode.OpenOrCreate;
Package.Open(path, mode, access);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(FileMode), mode))
    throw new InvalidDataException($"Undefined FileMode value: {mode}");

Type guard

bool IsDefinedFileMode(FileMode m) => Enum.IsDefined(typeof(FileMode), m);

Try / catch

try { pkg = Package.Open(path, mode, access); }
catch (ArgumentException ex) when (ex.Message.Contains("invalid")) { /* fix enum source */ }

Prevention

When it happens

Trigger: Calling StorageRoot.Open / Package.Open with a FileMode value not defined by System.IO.FileMode, typically from an invalid cast of an int or deserialized enum.

Common situations: Parsing mode strings from config into FileMode without validation; (FileMode)99 style casts; serialization round-trip corruption.

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


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

Appendix: source

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

            case FileMode.OpenOrCreate:
                {
                    FileInfo existTest = new FileInfo(path);
                    if( existTest.Exists )
                    {
                        // File exists, use open code path
                        goto case FileMode.Open;
                    }
                    else
                    {
                        // File does not exist, use create code path
                        goto case FileMode.Create;
                    }
                }
            case FileMode.Truncate:
                throw new ArgumentException(
                    SR.FileModeUnsupported);
            default:
                throw new ArgumentException(
                    SR.FileModeInvalid);
        }

        // Generate the access flags from the access parameter
        SafeNativeCompoundFileMethods.UpdateModeFlagFromFileAccess( access, ref grfMode );

        // Generate STGM from FileShare

        // Note: the .NET SDK does not specify the proper behavior in reaction to
        //  incompatible flags being sent in together.  Should ArgumentException be
        //  thrown?  Or do some values "trump" others?
        if( 0 != (share & FileShare.Inheritable) )
        {
            throw new ArgumentException(
                SR.FileShareUnsupported);
        }
        else if( share == FileShare.None ) // FileShare.None is zero, using "&" to check causes unreachable code error
        {

View on GitHub (pinned to 81131a70a4)