OSPlatform
OSPlatform represents a particular operating system platform
using System;
using System.Runtime.InteropServices;
namespace NUnit.Framework.Internal
{
public class OSPlatform
{
public enum ProductType
{
Unknown,
WorkStation,
DomainController,
Server
}
private struct OSVERSIONINFOEX
{
public uint dwOSVersionInfoSize;
public uint dwMajorVersion;
public uint dwMinorVersion;
public uint dwBuildNumber;
public uint dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szCSDVersion;
public short wServicePackMajor;
public short wServicePackMinor;
public short wSuiteMask;
public byte ProductType;
public byte Reserved;
}
private PlatformID platform;
private Version version;
private ProductType product;
private static OSPlatform currentPlatform;
public static readonly PlatformID UnixPlatformID_Microsoft = PlatformID.Unix;
public static readonly PlatformID UnixPlatformID_Mono = (PlatformID)128;
public static OSPlatform CurrentPlatform {
get {
if (currentPlatform == null) {
OperatingSystem oSVersion = Environment.OSVersion;
if (oSVersion.Platform == PlatformID.Win32NT && oSVersion.Version.Major >= 5) {
OSVERSIONINFOEX osvi = default(OSVERSIONINFOEX);
osvi.dwOSVersionInfoSize = (uint)Marshal.SizeOf((object)osvi);
GetVersionEx(ref osvi);
currentPlatform = new OSPlatform(oSVersion.Platform, oSVersion.Version, (ProductType)osvi.ProductType);
} else
currentPlatform = new OSPlatform(oSVersion.Platform, oSVersion.Version);
}
return currentPlatform;
}
}
public PlatformID Platform => platform;
public Version Version => version;
public ProductType Product => product;
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 != UnixPlatformID_Microsoft)
return platform == UnixPlatformID_Mono;
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 == PlatformID.Xbox;
public bool IsMacOSX => platform == PlatformID.MacOSX;
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;
}
}
[DllImport("Kernel32.dll")]
private static extern bool GetVersionEx(ref OSVERSIONINFOEX osvi);
public OSPlatform(PlatformID platform, Version version)
{
this.platform = platform;
this.version = version;
}
public OSPlatform(PlatformID platform, Version version, ProductType product)
: this(platform, version)
{
this.product = product;
}
}
}