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; } }
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; } } }
public static bool IsProfessional() { try { var info = new NativeMethods.OsVersionInfoEx(); info.Size = Marshal.SizeOf(info); if (!NativeMethods.GetVersionEx(ref info)) throw new Win32Exception(); var productType = NativeMethods.KnownProductTypes.Undefined; if (!NativeMethods.GetProductInfo(info.MajorVersion, info.MajorVersion, info.ServicePackMajor, info.ServicePackMinor, ref productType)) throw new Win32Exception(); return NativeMethods.ProfessionalProductTypes.Contains(productType); } catch (Exception e) { Log.WarnFormat("An exception occurred while attempting to determine whether this is a professional os: {0}", e.Message); return false; } }
/// <summary> /// Determines whether the computer is running Windows XP /// </summary> /// <returns></returns> public static bool IsWindowsXp() { if (Environment.OSVersion.Platform != PlatformID.Win32NT) return false; if (Environment.OSVersion.Version.Major != 5) return false; // 32-bit Windows XP if (Environment.OSVersion.Version.Minor == 1) return true; // something other than server version; return false if (Environment.OSVersion.Version.Minor != 2) return false; if (!Environment.Is64BitOperatingSystem) return false; var info = new NativeMethods.OsVersionInfoEx(); info.Size = Marshal.SizeOf(info); if (!NativeMethods.GetVersionEx(ref info)) return false; return info.ProductType != 11; }
public static int GetServicePackVersion() { try { var info = new NativeMethods.OsVersionInfoEx(); info.Size = Marshal.SizeOf(info); if (!NativeMethods.GetVersionEx(ref info)) throw new Win32Exception(); return Convert.ToInt32(info.ServicePackMajor); } catch (Exception e) { Log.WarnFormat("An issue occurred while attempting to determine this computer's service-pack version: {0}", e.Message); return 0; } }