OSPlatform
OSPlatform represents a particular operating system platform
using Microsoft.Win32;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
namespace NUnit.Framework.Internal
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class OSPlatform
{
[System.Runtime.CompilerServices.NullableContext(0)]
public enum ProductType
{
Unknown,
WorkStation,
DomainController,
Server
}
private static readonly Lazy<OSPlatform> LazyCurrentPlatform = new Lazy<OSPlatform>(delegate {
OperatingSystem operatingSystem = Environment.OSVersion;
if (operatingSystem.Platform == PlatformID.Win32NT && operatingSystem.Version.Major >= 5) {
if (OperatingSystem.IsWindows() && operatingSystem.Version.Major == 6 && operatingSystem.Version.Minor >= 2)
operatingSystem = new OperatingSystem(operatingSystem.Platform, GetWindows81PlusVersion(operatingSystem.Version));
ProductType productType = GetProductType();
return new OSPlatform(operatingSystem.Platform, operatingSystem.Version, productType);
}
if (!CheckIfIsMacOSX(operatingSystem.Platform))
return new OSPlatform(operatingSystem.Platform, operatingSystem.Version);
return new OSPlatform(PlatformID.MacOSX, operatingSystem.Version);
});
public static readonly PlatformID UnixPlatformIDMicrosoft = PlatformID.Unix;
public static readonly PlatformID UnixPlatformIDMono = (PlatformID)128;
public static readonly PlatformID XBoxPlatformID = PlatformID.Xbox;
public static readonly PlatformID MacOSXPlatformID = PlatformID.MacOSX;
public static OSPlatform CurrentPlatform => LazyCurrentPlatform.Value;
public PlatformID Platform { get; }
public Version Version { get; }
public ProductType Product { get; }
public bool IsWindows {
get {
if (Platform != PlatformID.Win32NT && Platform != PlatformID.Win32Windows && Platform != 0)
return Platform == PlatformID.WinCE;
return true;
}
}
public bool IsUnix {
get {
if (Platform != UnixPlatformIDMicrosoft)
return Platform == UnixPlatformIDMono;
return true;
}
}
public bool IsWin32S => Platform == PlatformID.Win32S;
public bool IsWin32Windows => Platform == PlatformID.Win32Windows;
public bool IsWin32NT => Platform == PlatformID.Win32NT;
public bool IsWinCE => Platform == PlatformID.WinCE;
public bool IsXbox => Platform == XBoxPlatformID;
public bool IsMacOSX => Platform == MacOSXPlatformID;
public bool IsWin95 {
get {
if (Platform == PlatformID.Win32Windows && Version.Major == 4)
return Version.Minor == 0;
return false;
}
}
public bool IsWin98 {
get {
if (Platform == PlatformID.Win32Windows && Version.Major == 4)
return Version.Minor == 10;
return false;
}
}
public bool IsWinME {
get {
if (Platform == PlatformID.Win32Windows && Version.Major == 4)
return Version.Minor == 90;
return false;
}
}
public bool IsNT3 {
get {
if (Platform == PlatformID.Win32NT)
return Version.Major == 3;
return false;
}
}
public bool IsNT4 {
get {
if (Platform == PlatformID.Win32NT)
return Version.Major == 4;
return false;
}
}
public bool IsNT5 {
get {
if (Platform == PlatformID.Win32NT)
return Version.Major == 5;
return false;
}
}
public bool IsWin2K {
get {
if (IsNT5)
return Version.Minor == 0;
return false;
}
}
public bool IsWinXP {
get {
if (IsNT5) {
if (Version.Minor != 1) {
if (Version.Minor == 2)
return Product == ProductType.WorkStation;
return false;
}
return true;
}
return false;
}
}
public bool IsWin2003Server {
get {
if (IsNT5 && Version.Minor == 2)
return Product == ProductType.Server;
return false;
}
}
public bool IsNT6 {
get {
if (Platform == PlatformID.Win32NT)
return Version.Major == 6;
return false;
}
}
public bool IsNT60 {
get {
if (IsNT6)
return Version.Minor == 0;
return false;
}
}
public bool IsNT61 {
get {
if (IsNT6)
return Version.Minor == 1;
return false;
}
}
public bool IsNT62 {
get {
if (IsNT6)
return Version.Minor == 2;
return false;
}
}
public bool IsNT63 {
get {
if (IsNT6)
return Version.Minor == 3;
return false;
}
}
public bool IsVista {
get {
if (IsNT60)
return Product == ProductType.WorkStation;
return false;
}
}
public bool IsWin2008Server {
get {
if (!IsWin2008ServerR1)
return IsWin2008ServerR2;
return true;
}
}
public bool IsWin2008ServerR1 {
get {
if (IsNT60)
return Product == ProductType.Server;
return false;
}
}
public bool IsWin2008ServerR2 {
get {
if (IsNT61)
return Product == ProductType.Server;
return false;
}
}
public bool IsWin2012Server {
get {
if (!IsWin2012ServerR1)
return IsWin2012ServerR2;
return true;
}
}
public bool IsWin2012ServerR1 {
get {
if (IsNT62)
return Product == ProductType.Server;
return false;
}
}
public bool IsWin2012ServerR2 {
get {
if (IsNT63)
return Product == ProductType.Server;
return false;
}
}
public bool IsWindows7 {
get {
if (IsNT61)
return Product == ProductType.WorkStation;
return false;
}
}
public bool IsWindows8 {
get {
if (IsNT62)
return Product == ProductType.WorkStation;
return false;
}
}
public bool IsWindows81 {
get {
if (IsNT63)
return Product == ProductType.WorkStation;
return false;
}
}
public bool IsWindows10 {
get {
if (Platform == PlatformID.Win32NT && Version.Major == 10 && Version.Minor < 22000)
return Product == ProductType.WorkStation;
return false;
}
}
public bool IsWindows11 {
get {
if (Platform == PlatformID.Win32NT && Version.Major == 10 && Version.Minor >= 22000)
return Product == ProductType.WorkStation;
return false;
}
}
public bool IsWindowsServer10 {
get {
if (Platform == PlatformID.Win32NT && Version.Major == 10)
return Product == ProductType.Server;
return false;
}
}
public static string OSDescription => RuntimeInformation.OSDescription;
[SupportedOSPlatform("windows")]
private static Version GetWindows81PlusVersion(Version version)
{
try {
using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion")) {
if (registryKey == null)
return version;
string s = registryKey.GetValue("CurrentBuildNumber") as string;
int.TryParse(s, out int result);
int? nullable = registryKey.GetValue("CurrentMajorVersionNumber") as int?;
int? nullable2 = registryKey.GetValue("CurrentMinorVersionNumber") as int?;
if (nullable.HasValue && nullable2.HasValue)
return new Version(nullable.Value, nullable2.Value, result);
string a = registryKey.GetValue("CurrentVersion") as string;
if (!(a == "6.3"))
return version;
return new Version(6, 3, result);
}
} catch (Exception) {
return version;
}
}
private static ProductType GetProductType()
{
if (OperatingSystem.IsWindows())
return GetWindowsProductType();
return ProductType.Unknown;
}
[SupportedOSPlatform("windows")]
private static ProductType GetWindowsProductType()
{
try {
using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion")) {
if (registryKey != null) {
switch (registryKey.GetValue("InstallationType") as string) {
case "Client":
return ProductType.WorkStation;
case "Server":
case "Server Core":
return ProductType.Server;
default:
return ProductType.Unknown;
}
}
}
} catch (Exception) {
}
return ProductType.Unknown;
}
public OSPlatform(PlatformID platform, Version version)
{
Platform = platform;
Version = version;
}
public OSPlatform(PlatformID platform, Version version, ProductType product)
: this(platform, version)
{
Product = product;
}
public override string ToString()
{
StringBuilder stringBuilder = new StringBuilder();
switch (Platform) {
case PlatformID.Win32NT:
stringBuilder.Append("Microsoft Windows NT");
break;
case PlatformID.Win32Windows:
stringBuilder.Append("Microsoft Windows 95/98");
break;
case PlatformID.Win32S:
stringBuilder.Append("Microsoft Windows Win32s");
break;
case PlatformID.WinCE:
stringBuilder.Append("Microsoft Windows CE");
break;
default:
stringBuilder.Append(Platform);
break;
}
stringBuilder.Append(" ").Append(Version);
return stringBuilder.ToString();
}
private static int UnameSafe(IntPtr buf)
{
try {
return uname(buf);
} catch (DllNotFoundException) {
return -1;
}
}
[DllImport("libc")]
private static extern int uname(IntPtr buf);
private static bool CheckIfIsMacOSX(PlatformID platform)
{
switch (platform) {
case PlatformID.MacOSX:
return true;
default:
return false;
case PlatformID.Unix: {
IntPtr intPtr = Marshal.AllocHGlobal(8192);
bool result = false;
if (UnameSafe(intPtr) == 0) {
string a = Marshal.PtrToStringAnsi(intPtr);
result = string.Equals(a, "Darwin");
}
Marshal.FreeHGlobal(intPtr);
return result;
}
}
}
}
}