Environment.OSVersion returns the correct operating system version
Environment.OSVersion returns the actual version of the operating system (OS) instead of, for example, the OS that's selected for application compatibility.
In previous .NET versions, Environment.OSVersion returns an OS version that may be incorrect when an application runs under Windows compatibility mode. For more information, see GetVersionExA function remarks. On macOS, Environment.OSVersion returns the underlying Darwin kernel version.
Starting in .NET 5, Environment.OSVersion returns the actual version of the operating system for Windows and macOS.
The following table shows the difference in behavior.
|Previous .NET versions||.NET 5+|
Reason for change
Users of this property expect it to return the actual version of the operating system. Most .NET apps don't specify their supported version in their application manifest, and thus get the default supported version from the dotnet host. As a result, the compatibility shim is rarely meaningful for the app that's running. When Windows releases a new version and an older dotnet host is still in use, these apps may get an incorrect OS version. Returning the actual version is more inline with developers' expectations of this API.
With the introduction of OperatingSystem.IsWindowsVersionAtLeast, OperatingSystem.IsMacOSVersionAtLeast, and System.Runtime.Versioning.SupportedOSPlatformAttribute in .NET 5, Environment.OSVersion was changed to be consistent for Windows and macOS.
Review and test any code that uses Environment.OSVersion to ensure it behaves as desired.
Submit and view feedback for