dotnet/wpf · warning · ArgumentException
is not a valid OS!
Error message
{osVer} is not a valid OS! What it means
OSVersionHelper.IsOsVersionOrGreater maps an OperatingSystemVersion enum value to the corresponding verify-version API. If the enum value falls outside every known case (an undefined or out-of-range cast value), it throws ArgumentException("{osVer} is not a valid OS!", nameof(osVer)). This guards internal callers against invalid enum values rather than silently returning false.
Solutions
- Only pass values from the OperatingSystemVersion enum — never cast arbitrary ints to it.
- Call OSVersionHelper.GetOsVersion() and compare the returned enum instead of hand-rolling version checks.
- Update WPF references so the enum and helper come from the same build.
- If extending the enum, add the matching case (and its IsOs*OrGreater property) to the switch.
Example fix
// before var ver = (OperatingSystemVersion)99; bool ok = OSVersionHelper.IsOsVersionOrGreater(ver); // throws ArgumentException // after bool ok = OSVersionHelper.IsOsVersionOrGreater(OperatingSystemVersion.Windows10RS5);
Defensive patterns
Strategy: type-guard
Validate before calling
static bool IsDefinedOsVersion(OperatingSystemVersion v) => Enum.IsDefined(typeof(OperatingSystemVersion), v);
Type guard
static bool TryGetOsCheck(OperatingSystemVersion v, out Func<bool> check)
{ check = null; if (!Enum.IsDefined(typeof(OperatingSystemVersion), v)) return false; check = () => OSVersionHelper.IsOsVersionOrGreater(v); return true; } Try / catch
try { ok = OSVersionHelper.IsOsVersionOrGreater(ver); }
catch (ArgumentException) { ok = false; /* unknown version value */ } Prevention
- Never cast raw ints to OperatingSystemVersion
- Use Enum.IsDefined before passing enum values across boundaries
- Keep WPF reference assemblies and runtime assemblies in sync
When it happens
Trigger: Passing an OperatingSystemVersion value not present in the switch (e.g. a value cast from an int that is not a defined enum member, or an enum member added by a newer WPF but handled by an older helper).
Common situations: Reflection/dynamic code casting raw ints to OperatingSystemVersion; code compiled against a different WPF build than the one loaded (enum member drift); copy-pasted switch logic in forks missing a newly added case.
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
- InvalidEnumArgumentException(value, (int) value…
- SR.Animation_UnrecognizedHandoffBehavior
- SR.Animation_UnrecognizedHandoffBehavior
- SR.DragDrop_DragDropEffectsInvalid
- SR.Format(SR.InputScope_InvalidInputScopeName, "value")
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/09a644d37b49121b.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Interop/OSVersionHelper.cs:248
case OperatingSystemVersion.Windows8:
return IsOsWindows8OrGreater;
case OperatingSystemVersion.Windows7SP1:
return IsOsWindows7SP1OrGreater;
case OperatingSystemVersion.Windows7:
return IsOsWindows7OrGreater;
case OperatingSystemVersion.WindowsVistaSP2:
return IsOsWindowsVistaSP2OrGreater;
case OperatingSystemVersion.WindowsVistaSP1:
return IsOsWindowsVistaSP1OrGreater;
case OperatingSystemVersion.WindowsVista:
return IsOsWindowsVistaOrGreater;
case OperatingSystemVersion.WindowsXPSP3:
return IsOsWindowsXPSP3OrGreater;
case OperatingSystemVersion.WindowsXPSP2:
return IsOsWindowsXPSP2OrGreater;
}
throw new ArgumentException($"{osVer} is not a valid OS!", nameof(osVer));
}
internal static OperatingSystemVersion GetOsVersion()
{
if (IsOsWindows10RS5OrGreater)
{
return OperatingSystemVersion.Windows10RS5;
}
else if (IsOsWindows10RS4OrGreater)
{
return OperatingSystemVersion.Windows10RS4;
}
else if (IsOsWindows10RS3OrGreater)
{
return OperatingSystemVersion.Windows10RS3;
}
else if (IsOsWindows10RS2OrGreater)
{View on GitHub (pinned to 81131a70a4)