Skip to content

Commit

Permalink
platformutils: use .NET Environment.OSVersion on CoreCLR
Browse files Browse the repository at this point in the history
Since .NET 5 we can now use the `Environment.OSVersion` property to
lookup the real OS version for macOS and Windows[1].

For .NET Framework (still used for our releases on Windows) we must
continue to use the Win32 API `RtlGetVersion` to avoid any Windows
compatibility nonsense.

We continue to use `uname` on Linux.

[1] https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/5.0/environment-osversion-returns-correct-version
  • Loading branch information
mjcheetham committed May 3, 2023
1 parent 73ddb8e commit 39504ac
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/shared/Core/PlatformUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,30 +351,24 @@ private static string GetOSType()

private static string GetOSVersion(ITrace2 trace2)
{
//
// Since .NET 5 we can use Environment.OSVersion because it was updated to
// return the correct version on Windows & macOS, rather than the manifested
// version for Windows or the kernel version for macOS.
// https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/5.0/environment-osversion-returns-correct-version
//
// However, we still need to use the old method for Windows on .NET Framework
// and call into the Win32 API to get the correct version (regardless of app
// compatibility settings).
#if NETFRAMEWORK
if (IsWindows() && RtlGetVersionEx(out RTL_OSVERSIONINFOEX osvi) == 0)
{
return $"{osvi.dwMajorVersion}.{osvi.dwMinorVersion} (build {osvi.dwBuildNumber})";
}

if (IsMacOS())
#endif
if (IsWindows() || IsMacOS())
{
var psi = new ProcessStartInfo
{
FileName = "/usr/bin/sw_vers",
Arguments = "-productVersion",
RedirectStandardOutput = true
};

using (var swvers = new ChildProcess(trace2, psi))
{
swvers.Start(Trace2ProcessClass.Other);
swvers.WaitForExit();

if (swvers.ExitCode == 0)
{
return swvers.StandardOutput.ReadToEnd().Trim();
}
}
return Environment.OSVersion.VersionString;
}

if (IsLinux())
Expand Down

0 comments on commit 39504ac

Please sign in to comment.