Skip to content

Commit 39504ac

Browse files
committed
platformutils: use .NET Environment.OSVersion on CoreCLR
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
1 parent 73ddb8e commit 39504ac

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

src/shared/Core/PlatformUtils.cs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -351,30 +351,24 @@ private static string GetOSType()
351351

352352
private static string GetOSVersion(ITrace2 trace2)
353353
{
354+
//
355+
// Since .NET 5 we can use Environment.OSVersion because it was updated to
356+
// return the correct version on Windows & macOS, rather than the manifested
357+
// version for Windows or the kernel version for macOS.
358+
// https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/5.0/environment-osversion-returns-correct-version
359+
//
360+
// However, we still need to use the old method for Windows on .NET Framework
361+
// and call into the Win32 API to get the correct version (regardless of app
362+
// compatibility settings).
363+
#if NETFRAMEWORK
354364
if (IsWindows() && RtlGetVersionEx(out RTL_OSVERSIONINFOEX osvi) == 0)
355365
{
356366
return $"{osvi.dwMajorVersion}.{osvi.dwMinorVersion} (build {osvi.dwBuildNumber})";
357367
}
358-
359-
if (IsMacOS())
368+
#endif
369+
if (IsWindows() || IsMacOS())
360370
{
361-
var psi = new ProcessStartInfo
362-
{
363-
FileName = "/usr/bin/sw_vers",
364-
Arguments = "-productVersion",
365-
RedirectStandardOutput = true
366-
};
367-
368-
using (var swvers = new ChildProcess(trace2, psi))
369-
{
370-
swvers.Start(Trace2ProcessClass.Other);
371-
swvers.WaitForExit();
372-
373-
if (swvers.ExitCode == 0)
374-
{
375-
return swvers.StandardOutput.ReadToEnd().Trim();
376-
}
377-
}
371+
return Environment.OSVersion.VersionString;
378372
}
379373

380374
if (IsLinux())

0 commit comments

Comments
 (0)