/// <summary>Determines whether the operating system is of the specified version or later, allowing specification of a minimum service pack that must be installed on the lowest version.</summary> /// <returns><see langword="true"/> if the operating system matches the specified <paramref name="version"/> with the specified service pack, or if the operating system is of a later version; otherwise, <see langword="false"/>.</returns> /// <param name="version">The minimum required version.</param> /// <param name="servicePackVersion">The major version of the service pack that must be installed on the minimum required version to return true. This can be 0 to indicate that no service pack is required.</param> public static bool IsAtLeast(EnumOsName version, int servicePackVersion) { return IsAtLeast(version) && ServicePackVersion.Major >= servicePackVersion; }
private static void UpdateData() { NativeMethods.OsVersionInfoEx verInfo = new NativeMethods.OsVersionInfoEx(); // Needed to prevent: System.Runtime.InteropServices.COMException: // The data area passed to a system call is too small. (Exception from HRESULT: 0x8007007A) verInfo.OSVersionInfoSize = Marshal.SizeOf(verInfo); NativeMethods.SystemInfo sysInfo = new NativeMethods.SystemInfo(); NativeMethods.GetSystemInfo(ref sysInfo); if (!NativeMethods.GetVersionEx(ref verInfo)) NativeError.ThrowException(Marshal.GetLastWin32Error()); Debug.Assert(verInfo.MajorVersion == Environment.OSVersion.Version.Major); Debug.Assert(verInfo.MinorVersion == Environment.OSVersion.Version.Minor); Debug.Assert(verInfo.BuildNumber == Environment.OSVersion.Version.Build); _processorArchitecture = (EnumProcessorArchitecture)sysInfo.processorArchitecture; _servicePackVersion = new Version(verInfo.ServicePackMajor, verInfo.ServicePackMinor); _isServer = verInfo.ProductType == NativeMethods.VerNtDomainController || verInfo.ProductType == NativeMethods.VerNtServer; // http://msdn.microsoft.com/en-us/library/windows/desktop/ms724833%28v=vs.85%29.aspx // The following table summarizes the most recent operating system version numbers. // Operating system Version number Other // ================================================================================ // Windows 8.1 6.3 OSVersionInfoEx.ProductType == VerNtWorkstation // Windows Server 2012 R2 6.3 OSVersionInfoEx.ProductType != VerNtWorkstation // Windows 8 6.2 OSVersionInfoEx.ProductType == VerNtWorkstation // Windows Server 2012 6.2 OSVersionInfoEx.ProductType != VerNtWorkstation // Windows 7 6.1 OSVersionInfoEx.ProductType == VerNtWorkstation // Windows Server 2008 R2 6.1 OSVersionInfoEx.ProductType != VerNtWorkstation // Windows Server 2008 6.0 OSVersionInfoEx.ProductType != VerNtWorkstation // Windows Vista 6.0 OSVersionInfoEx.ProductType == VerNtWorkstation // Windows Server 2003 R2 5.2 GetSystemMetrics(SM_SERVERR2) != 0 // Windows Home Server 5.2 OSVersionInfoEx.SuiteMask & VER_SUITE_WH_SERVER // Windows Server 2003 5.2 GetSystemMetrics(SM_SERVERR2) == 0 // Windows XP 64-Bit Edition 5.2 (OSVersionInfoEx.ProductType == VerNtWorkstation) && (sysInfo.PaName == PaName.X64) // Windows XP 5.1 Not applicable // Windows 2000 5.0 Not applicable if (verInfo.MajorVersion > 6) _enumOsName = EnumOsName.Later; else switch (verInfo.MajorVersion) { #region Version 6 case 6: switch (verInfo.MinorVersion) { // Windows Vista or Windows Server 2008 case 0: _enumOsName = (verInfo.ProductType == NativeMethods.VerNtWorkstation) ? EnumOsName.WindowsVista : EnumOsName.WindowsServer2008; break; // Windows 7 or Windows Server 2008 R2 case 1: _enumOsName = (verInfo.ProductType == NativeMethods.VerNtWorkstation) ? EnumOsName.Windows7 : EnumOsName.WindowsServer2008R2; break; // Windows 8 or Windows Server 2012 case 2: _enumOsName = (verInfo.ProductType == NativeMethods.VerNtWorkstation) ? EnumOsName.Windows8 : EnumOsName.WindowsServer2012; break; // Windows 8.1 or Windows Server 2012R2 case 3: _enumOsName = (verInfo.ProductType == NativeMethods.VerNtWorkstation) ? EnumOsName.Windows81 : EnumOsName.WindowsServer2012R2; break; default: _enumOsName = EnumOsName.Later; break; } break; #endregion // Version 6 #region Version 5 case 5: switch (verInfo.MinorVersion) { case 0: _enumOsName = EnumOsName.Windows2000; break; case 1: _enumOsName = EnumOsName.WindowsXP; break; case 2: _enumOsName = (verInfo.ProductType == NativeMethods.VerNtWorkstation && _processorArchitecture == EnumProcessorArchitecture.X64) ? EnumOsName.WindowsXP : (verInfo.ProductType != NativeMethods.VerNtWorkstation) ? EnumOsName.WindowsServer2003 : EnumOsName.Later; break; default: _enumOsName = EnumOsName.Later; break; } break; #endregion // Version 5 default: _enumOsName = EnumOsName.Earlier; break; } }
/// <summary>Determines whether the operating system is of the specified version or later.</summary> /// <returns><see langword="true"/> if the operating system is of the specified <paramref name="version"/> or later; otherwise, <see langword="false"/>.</returns> /// <param name="version">The lowest version for which to return true.</param> public static bool IsAtLeast(EnumOsName version) { return VersionName >= version; }
private static void UpdateData() { var verInfo = new NativeMethods.RTL_OSVERSIONINFOEXW(); // Needed to prevent: System.Runtime.InteropServices.COMException: // The data area passed to a system call is too small. (Exception from HRESULT: 0x8007007A) verInfo.dwOSVersionInfoSize = Marshal.SizeOf(verInfo); var sysInfo = new NativeMethods.SYSTEM_INFO(); NativeMethods.GetNativeSystemInfo(ref sysInfo); // RtlGetVersion returns STATUS_SUCCESS (0). var success = !NativeMethods.RtlGetVersion(ref verInfo); var lastError = Marshal.GetLastWin32Error(); if (!success) { throw new Win32Exception(lastError, "Function RtlGetVersion() failed to retrieve the operating system information."); } _osVersion = new Version(verInfo.dwMajorVersion, verInfo.dwMinorVersion, verInfo.dwBuildNumber); _processorArchitecture = sysInfo.wProcessorArchitecture; _servicePackVersion = new Version(verInfo.wServicePackMajor, verInfo.wServicePackMinor); _isServer = verInfo.wProductType == NativeMethods.VER_NT_DOMAIN_CONTROLLER || verInfo.wProductType == NativeMethods.VER_NT_SERVER; // RtlGetVersion: https://msdn.microsoft.com/en-us/library/windows/hardware/ff561910%28v=vs.85%29.aspx // Operating System Version: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx // The following table summarizes the most recent operating system version numbers. // Operating system Version number Other // ================================================================================ // Windows 10 10.0 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION // Windows Server 2016 10.0 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION // Windows 8.1 6.3 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION // Windows Server 2012 R2 6.3 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION // Windows 8 6.2 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION // Windows Server 2012 6.2 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION // Windows 7 6.1 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION // Windows Server 2008 R2 6.1 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION // Windows Server 2008 6.0 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION // Windows Vista 6.0 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION // Windows Server 2003 R2 5.2 GetSystemMetrics(SM_SERVERR2) != 0 // Windows Server 2003 5.2 GetSystemMetrics(SM_SERVERR2) == 0 // Windows XP 64-Bit Edition 5.2 (OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION) && (sysInfo.PaName == PaName.X64) // Windows XP 5.1 Not applicable // Windows 2000 5.0 Not applicable // 2017-01-07: 10 == The lastest MajorVersion of Windows. if (verInfo.dwMajorVersion > 10) { _enumOsName = EnumOsName.Later; } else { switch (verInfo.dwMajorVersion) { #region Version 10 case 10: // Windows 10 or Windows Server 2016 _enumOsName = verInfo.wProductType == NativeMethods.VER_NT_WORKSTATION ? EnumOsName.Windows10 : EnumOsName.WindowsServer2016; break; #endregion // Version 10 #region Version 6 case 6: switch (verInfo.dwMinorVersion) { // Windows 8.1 or Windows Server 2012 R2 case 3: _enumOsName = verInfo.wProductType == NativeMethods.VER_NT_WORKSTATION ? EnumOsName.Windows81 : EnumOsName.WindowsServer2012R2; break; // Windows 8 or Windows Server 2012 case 2: _enumOsName = verInfo.wProductType == NativeMethods.VER_NT_WORKSTATION ? EnumOsName.Windows8 : EnumOsName.WindowsServer2012; break; // Windows 7 or Windows Server 2008 R2 case 1: _enumOsName = verInfo.wProductType == NativeMethods.VER_NT_WORKSTATION ? EnumOsName.Windows7 : EnumOsName.WindowsServer2008R2; break; // Windows Vista or Windows Server 2008 case 0: _enumOsName = verInfo.wProductType == NativeMethods.VER_NT_WORKSTATION ? EnumOsName.WindowsVista : EnumOsName.WindowsServer2008; break; default: _enumOsName = EnumOsName.Later; break; } break; #endregion // Version 6 #region Version 5 case 5: switch (verInfo.dwMinorVersion) { case 2: _enumOsName = verInfo.wProductType == NativeMethods.VER_NT_WORKSTATION && _processorArchitecture == EnumProcessorArchitecture.X64 ? EnumOsName.WindowsXP : verInfo.wProductType != NativeMethods.VER_NT_WORKSTATION ? EnumOsName.WindowsServer2003 : EnumOsName.Later; break; case 1: _enumOsName = EnumOsName.WindowsXP; break; case 0: _enumOsName = EnumOsName.Windows2000; break; default: _enumOsName = EnumOsName.Later; break; } break; #endregion // Version 5 default: _enumOsName = EnumOsName.Earlier; break; } } }
/// <summary>Determines whether the operating system is of the specified version or later, allowing specification of a minimum service pack that must be installed on the lowest version.</summary> /// <returns><see langword="true"/> if the operating system matches the specified <paramref name="version"/> with the specified service pack, or if the operating system is of a later version; otherwise, <see langword="false"/>.</returns> /// <param name="version">The minimum required version.</param> /// <param name="servicePackVersion">The major version of the service pack that must be installed on the minimum required version to return true. This can be 0 to indicate that no service pack is required.</param> public static bool IsAtLeast(EnumOsName version, int servicePackVersion) { return(IsAtLeast(version) && ServicePackVersion.Major >= servicePackVersion); }
/// <summary>Determines whether the operating system is of the specified version or later.</summary> /// <returns><see langword="true"/> if the operating system is of the specified <paramref name="version"/> or later; otherwise, <see langword="false"/>.</returns> /// <param name="version">The lowest version for which to return true.</param> public static bool IsAtLeast(EnumOsName version) { return(VersionName >= version); }
private static void UpdateData() { NativeMethods.OsVersionInfoEx verInfo = new NativeMethods.OsVersionInfoEx(); // Needed to prevent: System.Runtime.InteropServices.COMException: // The data area passed to a system call is too small. (Exception from HRESULT: 0x8007007A) verInfo.OSVersionInfoSize = Marshal.SizeOf(verInfo); NativeMethods.SystemInfo sysInfo = new NativeMethods.SystemInfo(); NativeMethods.GetSystemInfo(ref sysInfo); if (!NativeMethods.GetVersionEx(ref verInfo)) { NativeError.ThrowException(Marshal.GetLastWin32Error()); } Debug.Assert(verInfo.MajorVersion == Environment.OSVersion.Version.Major); Debug.Assert(verInfo.MinorVersion == Environment.OSVersion.Version.Minor); Debug.Assert(verInfo.BuildNumber == Environment.OSVersion.Version.Build); _processorArchitecture = (EnumProcessorArchitecture)sysInfo.processorArchitecture; _servicePackVersion = new Version(verInfo.ServicePackMajor, verInfo.ServicePackMinor); _isServer = verInfo.ProductType == NativeMethods.VerNtDomainController || verInfo.ProductType == NativeMethods.VerNtServer; // http://msdn.microsoft.com/en-us/library/windows/desktop/ms724833%28v=vs.85%29.aspx // The following table summarizes the most recent operating system version numbers. // Operating system Version number Other // ================================================================================ // Windows 8.1 6.3 OSVersionInfoEx.ProductType == VerNtWorkstation // Windows Server 2012 R2 6.3 OSVersionInfoEx.ProductType != VerNtWorkstation // Windows 8 6.2 OSVersionInfoEx.ProductType == VerNtWorkstation // Windows Server 2012 6.2 OSVersionInfoEx.ProductType != VerNtWorkstation // Windows 7 6.1 OSVersionInfoEx.ProductType == VerNtWorkstation // Windows Server 2008 R2 6.1 OSVersionInfoEx.ProductType != VerNtWorkstation // Windows Server 2008 6.0 OSVersionInfoEx.ProductType != VerNtWorkstation // Windows Vista 6.0 OSVersionInfoEx.ProductType == VerNtWorkstation // Windows Server 2003 R2 5.2 GetSystemMetrics(SM_SERVERR2) != 0 // Windows Home Server 5.2 OSVersionInfoEx.SuiteMask & VER_SUITE_WH_SERVER // Windows Server 2003 5.2 GetSystemMetrics(SM_SERVERR2) == 0 // Windows XP 64-Bit Edition 5.2 (OSVersionInfoEx.ProductType == VerNtWorkstation) && (sysInfo.PaName == PaName.X64) // Windows XP 5.1 Not applicable // Windows 2000 5.0 Not applicable if (verInfo.MajorVersion > 6) { _enumOsName = EnumOsName.Later; } else { switch (verInfo.MajorVersion) { #region Version 6 case 6: switch (verInfo.MinorVersion) { // Windows Vista or Windows Server 2008 case 0: _enumOsName = (verInfo.ProductType == NativeMethods.VerNtWorkstation) ? EnumOsName.WindowsVista : EnumOsName.WindowsServer2008; break; // Windows 7 or Windows Server 2008 R2 case 1: _enumOsName = (verInfo.ProductType == NativeMethods.VerNtWorkstation) ? EnumOsName.Windows7 : EnumOsName.WindowsServer2008R2; break; // Windows 8 or Windows Server 2012 case 2: _enumOsName = (verInfo.ProductType == NativeMethods.VerNtWorkstation) ? EnumOsName.Windows8 : EnumOsName.WindowsServer2012; break; // Windows 8.1 or Windows Server 2012R2 case 3: _enumOsName = (verInfo.ProductType == NativeMethods.VerNtWorkstation) ? EnumOsName.Windows81 : EnumOsName.WindowsServer2012R2; break; default: _enumOsName = EnumOsName.Later; break; } break; #endregion // Version 6 #region Version 5 case 5: switch (verInfo.MinorVersion) { case 0: _enumOsName = EnumOsName.Windows2000; break; case 1: _enumOsName = EnumOsName.WindowsXP; break; case 2: _enumOsName = (verInfo.ProductType == NativeMethods.VerNtWorkstation && _processorArchitecture == EnumProcessorArchitecture.X64) ? EnumOsName.WindowsXP : (verInfo.ProductType != NativeMethods.VerNtWorkstation) ? EnumOsName.WindowsServer2003 : EnumOsName.Later; break; default: _enumOsName = EnumOsName.Later; break; } break; #endregion // Version 5 default: _enumOsName = EnumOsName.Earlier; break; } } }