dotnet/wpf · error · ArgumentOutOfRangeException
SeekOrigin value is not valid.
Error message
SeekOrigin value is not valid.
What it means
RightsManagementEncryptedStream.Seek supports only SeekOrigin.Begin, SeekOrigin.Current, and SeekOrigin.End. A value outside the defined SeekOrigin enum hits the switch's default case and throws ArgumentOutOfRangeException with the 'SeekOrigin value is not valid' message.
Solutions
- Only pass SeekOrigin.Begin, SeekOrigin.Current, or SeekOrigin.End — never raw ints cast to SeekOrigin.
- Map native seek constants to the correct SeekOrigin member before calling.
- Catch ArgumentOutOfRangeException to detect a bad origin value and log it.
Example fix
// before stream.Seek(offset, (SeekOrigin)seekConst); // interop constant // after var origin = seekConst == 0 ? SeekOrigin.Begin : seekConst == 1 ? SeekOrigin.Current : SeekOrigin.End; stream.Seek(offset, origin);
Defensive patterns
Strategy: type-guard
Validate before calling
bool IsValidOrigin(SeekOrigin o) => o is SeekOrigin.Begin or SeekOrigin.Current or SeekOrigin.End;
Type guard
static bool TryGetSeekOrigin(int raw, out SeekOrigin origin) {
switch (raw) {
case 0: origin = SeekOrigin.Begin; return true;
case 1: origin = SeekOrigin.Current; return true;
case 2: origin = SeekOrigin.End; return true;
default: origin = default; return false;
}
} Try / catch
try { stream.Seek(offset, origin); }
catch (ArgumentOutOfRangeException ex) { /* ex.ParamName == "origin"; map native constant and retry */ } Prevention
- Never cast raw ints to SeekOrigin; map native seek constants explicitly
- Use Enum.IsDefined(typeof(SeekOrigin), value) before casting
- Centralize seek-origin translation in one helper for interop code
When it happens
Trigger: Calling Seek with an origin value not in the SeekOrigin enum, e.g. stream.Seek(offset, (SeekOrigin)3) — commonly via a cast from an int/interop value.
Common situations: Interop code passing native SEEK_SET/SEEK_CUR/SEEK_END values (0/1/2 semantics may differ) or integer flags cast to SeekOrigin without mapping.
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 set seek pointer to a negative position.
- SeekOrigin value is not valid.
- SR.SeekNegative
- SR.SeekOriginInvalid
- ArgumentOutOfRangeException(authenticationType)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/9bd01b4d06b444c0.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/RightsManagementEncryptedStream.cs:142
{
case SeekOrigin.Begin:
{
temp = offset;
break;
}
case SeekOrigin.Current:
{
temp = _streamPosition + offset;
break;
}
case SeekOrigin.End:
{
temp = Length + offset;
break;
}
default:
{
throw new ArgumentOutOfRangeException(nameof(origin), SR.SeekOriginInvalid);
}
}
}
if (temp < 0)
{
throw new ArgumentOutOfRangeException(nameof(offset), SR.SeekNegative);
}
_streamPosition = temp;
return _streamPosition;
}
/// <summary>
/// See .NET Framework SDK under System.IO.Stream
/// </summary>
public override void SetLength(long newLength)
{View on GitHub (pinned to 81131a70a4)