public OSVersionInfo() { osVersionInfo = new OSVERSIONINFOEX { dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)) }; GetVersionEx(ref osVersionInfo); }
/// <summary> /// 获取当前操作系统信息 /// </summary> /// <returns></returns> public static OSVersionInfoModel GetOSVersion() { OSVersionInfoModel m = null; OSVERSIONINFOEX vm = new OSVERSIONINFOEX(); vm.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)); if (GetVersionEx(ref vm)) { m = new OSVersionInfoModel() { BuildNumber = vm.dwBuildNumber, MajorVersion = vm.dwMajorVersion, MinorVersion = vm.dwMinorVersion, PlatformId = vm.dwPlatformId, CSDVersion = vm.szCSDVersion, ProductType = vm.wProductType, Reserved = vm.wReserved, ServicePackMajor = vm.wServicePackMajor, ServicePackMinor = vm.wServicePackMinor, SuiteMask = vm.wSuiteMask, Name = GetOSName(ref vm) }; } return(m); }
public static Version RtlGetVersion() { var ver = new OSVERSIONINFOEX(); if (!RtlGetVersion(ref ver) && ver.MajorVersion < 7) { return(default);
/// <summary> /// Loads Windows version information. /// </summary> private void InitWindows() { OSVERSIONINFOEX info = new OSVERSIONINFOEX(); info.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)); if (!WinApi.GetVersionEx(ref info)) { throw new ApplicationException("WinApi.GetVersionEx() call failed."); } this.oSVersion = new Version(info.dwMajorVersion, info.dwMinorVersion, info.dwBuildNumber); this.backOffice = (info.wSuiteMask & (int)WinApi.OsSuite.VER_SUITE_BACKOFFICE) != 0; this.webEdition = (info.wSuiteMask & (int)WinApi.OsSuite.VER_SUITE_BLADE) != 0; this.computeCluster = (info.wSuiteMask & (int)WinApi.OsSuite.VER_SUITE_COMPUTE_SERVER) != 0; this.datacenterEdition = (info.wSuiteMask & (int)WinApi.OsSuite.VER_SUITE_DATACENTER) != 0; this.enterpriseEdition = (info.wSuiteMask & (int)WinApi.OsSuite.VER_SUITE_ENTERPRISE) != 0; this.embeddedXP = (info.wSuiteMask & (int)WinApi.OsSuite.VER_SUITE_EMBEDDEDNT) != 0; this.personal = (info.wSuiteMask & (int)WinApi.OsSuite.VER_SUITE_PERSONAL) != 0; this.singleUserTerminalServices = (info.wSuiteMask & (int)WinApi.OsSuite.VER_SUITE_SINGLEUSERTS) != 0; this.smallBusinessEdition = (info.wSuiteMask & (int)WinApi.OsSuite.VER_SUITE_SMALLBUSINESS) != 0; this.smallBusinessRestricted = (info.wSuiteMask & (int)WinApi.OsSuite.VER_SUITE_SMALLBUSINESS_RESTRICTED) != 0; this.storageServer = (info.wSuiteMask & (int)WinApi.OsSuite.VER_SUITE_STORAGE_SERVER) != 0; this.terminalServices = (info.wSuiteMask & (int)WinApi.OsSuite.VER_SUITE_TERMINAL) != 0; this.homeServer = (info.wSuiteMask & (int)WinApi.OsSuite.VER_SUITE_WH_SERVER) != 0; this.server = info.wProductType == (int)WinApi.OsProduct.VER_NT_SERVER || info.wProductType == (int)WinApi.OsProduct.VER_NT_DOMAIN_CONTROLLER; this.workstation = info.wProductType == (int)WinApi.OsProduct.VER_NT_WORKSTATION; this.activeDirectory = info.wProductType == (int)WinApi.OsProduct.VER_NT_DOMAIN_CONTROLLER; this.mediaCenter = WinApi.GetSystemMetrics((int)SystemMetricsCodes.SM_MEDIACENTER) != 0; this.tabletPC = WinApi.GetSystemMetrics((int)SystemMetricsCodes.SM_TABLETPC) != 0; this.servicePack = info.szCSDVersion; }
/// <summary> /// Gets the name of the Windows version /// </summary> /// <param name="version">The version.</param> /// <remarks>Looked at http://www.csharp411.com/determine-windows-version-and-edition-with-c/</remarks> private static string get_windows_name(Version version) { var name = "Windows"; var isServer = false; var osVersionInfo = new OSVERSIONINFOEX(); osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)); var success = GetVersionEx(ref osVersionInfo); if (success) { isServer = osVersionInfo.wProductType == ServerNT; } //https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832.aspx //switch doesn't like a double, but a string is fine?! string majorMinor = version.Major + "." + version.Minor; switch (majorMinor) { case "6.4": name = isServer ? "Windows Server 2016 (?)" : "Windows 10"; break; case "6.3": name = isServer ? "Windows Server 2012 R2" : "Windows 8.1"; break; case "6.2": name = isServer ? "Windows Server 2012" : "Windows 8"; break; case "6.1": name = isServer ? "Windows Server 2008 R2" : "Windows 7"; break; case "6.0": name = isServer ? "Windows Server 2008" : "Windows Vista"; break; case "5.2": name = isServer ? "Windows Server 2003" : "Windows XP"; break; case "5.1": name = "Windows XP"; break; case "5.0": name = "Windows 2000"; break; } return(name); }
unsafe public static int GetOsVersion() { OSVERSIONINFOEX os = new OSVERSIONINFOEX(); int nRet = 0; os.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)); // SIZEOF if (GetVersionEx(ref os)) { switch (os.dwPlatformId) { case 1: nRet = os.dwPlatformId; switch (os.dwMinorVersion) { case 0: nRet = 95; // 95 break; case 10: nRet = 98; // 98 break; case 90: nRet = 100; // ME break; } break; case 2: nRet = os.dwPlatformId; switch (os.dwMajorVersion) { case 3: nRet = 351; // NT 3.51 break; case 4: nRet = 400; // NT 4.0 break; case 5: nRet = 500; // 2000 if (os.dwMinorVersion == 1) { nRet = 501; // XP } break; case 6: nRet = 600; // VISTA break; } break; default: nRet = -1; break; } } return(nRet); }
private static unsafe uint GetWindowsMinorVersion() { OSVERSIONINFOEX osvi = default; osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); bool result = GetVersionEx(ref osvi); Debug.Assert(result); return(osvi.dwMinorVersion); }
/*----------------------------------------------------------------------------- * IsWindowsXP_orLater() * * Description: * Determines if the currently-running version of Windows is Windows XP or * Windows Server 2003 or later. * * Return Value: * Returns true if the currently-running system is Windows XP or Windows 2003 * Server or later systems. Returns false otherwise. * -----------------------------------------------------------------------------*/ private static bool IsWindowsXP_orLater() { var comparisonMask = VerSetConditionMask(0, VERSION_MASK.VER_MAJORVERSION, VERSION_CONDITION.VER_GREATER_EQUAL); comparisonMask = VerSetConditionMask(comparisonMask, VERSION_MASK.VER_MINORVERSION, VERSION_CONDITION.VER_GREATER_EQUAL); var osvi = new OSVERSIONINFOEX { dwOSVersionInfoSize = (uint)Marshal.SizeOf <OSVERSIONINFOEX>(), dwMajorVersion = 5, dwMinorVersion = 1 }; return(VerifyVersionInfo(ref osvi, VERSION_MASK.VER_MAJORVERSION | VERSION_MASK.VER_MINORVERSION, comparisonMask)); }
// Static Constructor static WinVersion() { osvi = new OSVERSIONINFOEX(); osvi.dwOSVersionInfoSize = (uint)Marshal.SizeOf( typeof(OSVERSIONINFOEX) ); GetVersionEx(ref osvi); _IsWOW64(); _Is64BitOS(); }
static NativeVersion() { OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX { OSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)) }; if (RtlGetVersion(ref osVersionInfo) != 0) { throw new Exception("Failed to call RtlGetVersion"); } m_KernelVersion = (float)osVersionInfo.MajorVersion + (float)osVersionInfo.MinorVersion; }
private static OSVERSIONINFOEX QueryWindowsVersion() { var lWinVer = new OSVERSIONINFOEX(); lWinVer.dwOSVersionInfoSize = Marshal.SizeOf(lWinVer); if (!GetVersionEx(ref lWinVer)) { throw new Win32Exception(); } return(lWinVer); }
public static bool GetSystemTheme(OSVERSIONINFOEX osInfo) { if (osInfo.MajorVersion < 10 || osInfo.BuildNumber < 17763) //1809 { return(false); } if (osInfo.BuildNumber < 18362) //1903 { return(fnShouldAppsUseDarkMode()); } return(fnShouldSystemUseDarkMode()); }
public bool IsWindows10() { OperatingSystem OS = Environment.OSVersion; OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX(); osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)); GetVersionEx(ref osVersionInfo); if (osVersionInfo.dwMajorVersion == 10) { return(true); } return(false); }
public static void RtlGetVersion(ref OSVERSIONINFOEX VersionInformation) { object[] funcargs = { VersionInformation }; NTSTATUS retValue = (NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"RtlGetVersion", typeof(DELEGATES.RtlGetVersion), ref funcargs); if (retValue != NTSTATUS.Success) { throw new InvalidOperationException("Failed get procedure address, " + retValue); } VersionInformation = (OSVERSIONINFOEX)funcargs[0]; }
/// <summary> /// Returns the service pack information of the operating system running on this computer. /// </summary> /// <returns>A string containing the the operating system service pack information.</returns> public static string GetOSServicePack() { var osVersionInfo = new OSVERSIONINFOEX() { dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)) }; if (!GetVersionEx(ref osVersionInfo)) { return(String.Empty); } else { return(OasStringConstants.Space + osVersionInfo.szCSDVersion); } }
public static bool ApplyTheme(IntPtr hwnd, bool useDark, OSVERSIONINFOEX osInfo) { if (osInfo.MajorVersion < 10 || osInfo.BuildNumber < 17763) //1809 { return(false); } if (osInfo.BuildNumber < 18362) //1903 { var res = fnAllowDarkModeForApp(hwnd, useDark); if (res == false) { return(res); } int dark = useDark ? 1 : 0; DwmSetWindowAttribute(hwnd, DWMWINDOWATTRIBUTE.UseImmersiveDarkMode, ref dark, Marshal.SizeOf <int>()); } else { //Not sure what a successful return value is on this one fnSetPreferredAppMode(hwnd, useDark ? PreferredAppMode.AllowDark : PreferredAppMode.Default); fnRefreshImmersiveColorPolicyState(); int success = 0; unsafe { WINDOWCOMPOSITIONATTRIBDATA data = new WINDOWCOMPOSITIONATTRIBDATA { attrib = WINDOWCOMPOSITIONATTRIB.WCA_USEDARKMODECOLORS, data = &useDark, sizeOfData = sizeof(int) }; success = SetWindowCompositionAttribute(hwnd, &data); } if (success == 0) { return(false); } } return(true); }
static string AutoSelect(string asfile) { if (!File.Exists(asfile)) return null; string[] lines = File.ReadAllLines(asfile); OSVERSIONINFOEX osvi = new OSVERSIONINFOEX(); osvi.dwOSVersionInfoSize = (uint)Marshal.SizeOf(osvi); GetVersionEx(ref osvi); string cpuarch = Enum.GetName(typeof(CpuArchitecture), Configuration.RealCpuArch); string result = null; foreach (string l in lines) { string[] items = l.Split(' '); uint val = 0; if (items.Length < 5) continue; if (!UInt32.TryParse(items[0], out val) || val != osvi.dwMajorVersion) continue; else if (!UInt32.TryParse(items[1], out val) || val != osvi.dwMinorVersion) continue; else if (!UInt32.TryParse(items[2], out val) || val != osvi.wProductType) continue; else if (items[3] != cpuarch) continue; result = items[4]; break; } return result; }
/// <summary> /// Returns the name of the operating system running on this computer. /// </summary> /// <returns>A string containing the the operating system name.</returns> public static string GetOSName() { OperatingSystem osInfo = Environment.OSVersion; OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX(); osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)); string osName = "UNKNOWN"; bool x64Detection = false; if (!GetVersionEx(ref osVersionInfo)) { return ""; } else { switch (osInfo.Platform) { case PlatformID.Win32Windows: { switch (osInfo.Version.Minor) { case 0: osName = "Windows 95"; break; case 10: { if (osInfo.Version.Revision.ToString() == "2222A") osName = "Windows 98 Second Edition"; else osName = "Windows 98"; } break; case 90: osName = "Windows Me"; break; } break; } case PlatformID.Win32NT: { switch (osInfo.Version.Major) { case 3: osName = "Windows NT 3.51"; break; case 4: { switch (osVersionInfo.wProductType) { case 1: osName = "Windows NT 4.0 Workstation"; break; case 3: osName = "Windows NT 4.0 Server"; break; } break; } case 5: { switch (osInfo.Version.Minor) { case 0: // win2K { if ((osVersionInfo.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER) osName = "Windows 2000 Datacenter Server"; else if ((osVersionInfo.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE) osName = "Windows 2000 Advanced Server"; else osName = "Windows 2000"; break; } case 1: // winXP { if ((osVersionInfo.wSuiteMask & VER_SUITE_PERSONAL) == VER_SUITE_PERSONAL) osName = "Windows XP Home Edition"; else osName = "Windows XP Professional"; x64Detection = true; break; } case 2: // winserver 2003 { if ((osVersionInfo.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER) osName = "Windows Server 2003 DataCenter Edition"; else if ((osVersionInfo.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE) osName = "Windows Server 2003 Enterprise Edition"; else if ((osVersionInfo.wSuiteMask & VER_SUITE_BLADE) == VER_SUITE_BLADE) osName = "Windows Server 2003 Web Edition"; else osName = "Windows Server 2003 Standard Edition"; x64Detection = true; break; } } break; } case 6: { x64Detection = true; switch (osInfo.Version.Minor) { case 0: { switch (osVersionInfo.wProductType) { case 1: // Vista { uint edition = PRODUCT_UNDEFINED; if (GetProductInfo(osVersionInfo.dwMajorVersion, osVersionInfo.dwMinorVersion, osVersionInfo.wServicePackMajor, osVersionInfo.wServicePackMinor, out edition)) { switch (edition) { case PRODUCT_ULTIMATE: osName = "Windows Vista Ultimate Edition"; break; case PRODUCT_HOME_BASIC: case PRODUCT_HOME_BASIC_N: osName = "Windows Vista Home Basic Edition"; break; case PRODUCT_HOME_PREMIUM: osName = "Windows Vista Premium Edition"; break; case PRODUCT_ENTERPRISE: osName = "Windows Vista Enterprise Edition"; break; case PRODUCT_BUSINESS: case PRODUCT_BUSINESS_N: osName = "Windows Vista Business Edition"; break; case PRODUCT_STARTER: osName = "Windows Vista Starter Edition"; break; default: osName = "Windows Vista"; break; } } break; } case 3: // Server 2008 { if ((osVersionInfo.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER) osName = "Windows Server 2008 Datacenter Server"; else if ((osVersionInfo.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE) osName = "Windows Server 2008 Advanced Server"; else osName = "Windows Server 2008"; break; } } break; } case 1: // Se7en { uint edition = PRODUCT_UNDEFINED; if (GetProductInfo(osVersionInfo.dwMajorVersion, osVersionInfo.dwMinorVersion, osVersionInfo.wServicePackMajor, osVersionInfo.wServicePackMinor, out edition)) { switch (edition) { case PRODUCT_ULTIMATE: osName = "Windows Seven Ultimate Edition"; break; case PRODUCT_HOME_BASIC: case PRODUCT_HOME_BASIC_N: osName = "Windows Seven Home Basic Edition"; break; case PRODUCT_HOME_PREMIUM: osName = "Windows Seven Premium Edition"; break; case PRODUCT_ENTERPRISE: osName = "Windows Seven Enterprise Edition"; break; case PRODUCT_BUSINESS: case PRODUCT_BUSINESS_N: osName = "Windows Seven Professional Edition"; break; case PRODUCT_STARTER: osName = "Windows Seven Starter Edition"; break; default: osName = "Windows Seven"; break; } } break; } } break; } } break; } } } osName += " x64"; if (x64Detection) { if (!isWow64()) osName += " x86"; else osName += " x64"; } return osName; }
private string GetOperatingSystemName() { string OSName = "Unknown"; System.OperatingSystem osInfo = System.Environment.OSVersion; if (osInfo.Platform == PlatformID.Unix) { return Environment.OSVersion.ToString(); } OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX(); osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)); if (!GetVersionEx(ref osVersionInfo)) { return OSName; } switch (osInfo.Platform) { case PlatformID.Win32NT: switch (osInfo.Version.Major) { case 3: OSName = "Windows NT 3.51"; break; case 4: OSName = "Windows NT 4.0"; break; case 5: switch (osInfo.Version.Minor) { case 0: OSName = "Windows 2000"; break; case 1: OSName = "Windows XP"; break; case 2: OSName = "Windows Server 2003"; break; default: break; } break; case 6: switch (osInfo.Version.Minor) { case 0: //producttype == VER_NT_WORKSTATION if (osVersionInfo.dwPlatformId != VER_NT_WORKSTATION) OSName = "Windows Vista"; else //producttype != VER_NT_WORKSTATION OSName = "Windows Server 2008"; break; case 1: //producttype != VER_NT_WORKSTATION if (osVersionInfo.dwPlatformId == VER_NT_WORKSTATION) OSName = "Windws Server 2008 R2"; else //producttype == VER_NT_WORKSTATION OSName = "Windows 7"; break; } break; default: OSName = "Unknown Win32NT Windows"; break; } break; case PlatformID.Win32S: break; case PlatformID.Win32Windows: switch (osInfo.Version.Major) { case 0: OSName = "Windows 95"; break; case 10: if (osInfo.Version.Revision.ToString() == "2222A") OSName = "Windows 98 Second Edition"; else OSName = "Windows 98"; break; case 90: OSName = "Windows ME"; break; default: OSName = "Unknown Win32 Windows"; break; } break; case PlatformID.WinCE: break; default: break; } return OSName; }
/// <summary> /// Returns the product type of the operating system running on this computer. /// </summary> /// <returns>A string containing the the operating system product type.</returns> public static string GetOSProductType() { OSVERSIONINFOEX versionInfo = new OSVERSIONINFOEX(); OperatingSystem info = System.Environment.OSVersion; versionInfo.VersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)); if (!GetVersionEx(ref versionInfo)) { return string.Empty; } else { if (info.Version.Major == 4) { if (versionInfo.ProductType == VERNTWORKSTATION) { // Windows NT 4.0 Workstation return " Workstation"; } else if (versionInfo.ProductType == VERNTSERVER) { // Windows NT 4.0 Server return " Server"; } else { return string.Empty; } } else if (info.Version.Major == 5) { if (versionInfo.ProductType == VERNTWORKSTATION) { if ((versionInfo.SuiteMask & VERSUITEPERSONAL) == VERSUITEPERSONAL) { // Windows XP Home Edition return " Home Edition"; } else { // Windows XP / Windows 2000 Professional return " Professional"; } } else if (versionInfo.ProductType == VERNTSERVER) { if (info.Version.Minor == 0) { if ((versionInfo.SuiteMask & VERSUITEDATACENTER) == VERSUITEDATACENTER) { // Windows 2000 Datacenter Server return " Datacenter Server"; } else if ((versionInfo.SuiteMask & VERSUITEENTERPRISE) == VERSUITEENTERPRISE) { // Windows 2000 Advanced Server return " Advanced Server"; } else { // Windows 2000 Server return " Server"; } } else { if ((versionInfo.SuiteMask & VERSUITEDATACENTER) == VERSUITEDATACENTER) { // Windows Server 2003 Datacenter Edition return " Datacenter Edition"; } else if ((versionInfo.SuiteMask & VERSUITEENTERPRISE) == VERSUITEENTERPRISE) { // Windows Server 2003 Enterprise Edition return " Enterprise Edition"; } else if ((versionInfo.SuiteMask & VERSUITEBLADE) == VERSUITEBLADE) { // Windows Server 2003 Web Edition return " Web Edition"; } else { // Windows Server 2003 Standard Edition return " Standard Edition"; } } } } } return string.Empty; }
/// <summary> /// Checks whether the OS version reported via GetVersionEx matches that of VerifyVersionInfo /// When running in compatibility mode GetVersionEx can return the value of the /// compatibility setting rather than the actual OS /// </summary> /// <param name="majorVersion">Reported OS Major Version</param> /// <param name="minorVersion">Reported OS Minor Version</param> /// <param name="buildVersion">Reported OS Build Version</param> /// <param name="productType">Reported OS Product Type</param> /// <param name="servicePack">Reported OS Major Service Pack Version</param> /// <returns>True if actual OS matches reported one</returns> private static bool IsOSAsReported(int majorVersion, int minorVersion, int buildVersion, byte productType, short servicePack) { ulong condition = 0; var osVersionInfo = new OSVERSIONINFOEX { dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)), dwMajorVersion = majorVersion, dwMinorVersion = minorVersion, dwBuildNumber = buildVersion, wProductType = productType, wServicePackMajor = servicePack }; condition = VerSetConditionMask(condition, VER_MAJORVERSION, VER_EQUAL); condition = VerSetConditionMask(condition, VER_MINORVERSION, VER_EQUAL); condition = VerSetConditionMask(condition, VER_PRODUCT_TYPE, VER_EQUAL); condition = VerSetConditionMask(condition, VER_SERVICEPACKMAJOR, VER_EQUAL); condition = VerSetConditionMask(condition, VER_BUILDVERSION, VER_EQUAL); return VerifyVersionInfo(ref osVersionInfo, VER_MAJORVERSION | VER_MINORVERSION | VER_PRODUCT_TYPE | VER_SERVICEPACKMAJOR | VER_BUILDVERSION, condition); }
private static extern bool VerifyVersionInfo(ref OSVERSIONINFOEX osVersionInfo, [In] uint dwTypeMask, [In] UInt64 dwlConditionMask);
/// <summary> /// Returns the product type of the operating system running on this computer. /// </summary> /// <returns>A string containing the the operating system product type.</returns> public static string GetOSProductType() { OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX(); osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof (OSVERSIONINFOEX)); if (!GetVersionEx(ref osVersionInfo)) return string.Empty; switch (OSMajorVersion) { case 4: if (OSProductType == NT_WORKSTATION) { // Windows NT 4.0 Workstation return " Workstation"; } if (OSProductType == NT_SERVER) { // Windows NT 4.0 Server return " Server"; } return string.Empty; case 5: if (GetSystemMetrics(SM_MEDIACENTER)) { return " Media Center"; } if (GetSystemMetrics(SM_TABLETPC)) { return " Tablet PC"; } if (OSProductType == NT_WORKSTATION) { if ((osVersionInfo.wSuiteMask & VER_SUITE_EMBEDDEDNT) == VER_SUITE_EMBEDDEDNT) { //Windows XP Embedded return " Embedded"; } return (osVersionInfo.wSuiteMask & VER_SUITE_PERSONAL) == VER_SUITE_PERSONAL ? " Home" : " Professional"; // Windows XP / Windows 2000 Professional } if (OSProductType == NT_SERVER || OSProductType == NT_DOMAIN_CONTROLLER) { if (OSMinorVersion == 0) { if ((osVersionInfo.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER) { // Windows 2000 Datacenter Server return " Datacenter Server"; } if ((osVersionInfo.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE) { // Windows 2000 Advanced Server return " Advanced Server"; } // Windows 2000 Server return " Server"; } if (OSMinorVersion == 2) { if ((osVersionInfo.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER) { // Windows Server 2003 Datacenter Edition return " Datacenter Edition"; } if ((osVersionInfo.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE) { // Windows Server 2003 Enterprise Edition return " Enterprise Edition"; } if ((osVersionInfo.wSuiteMask & VER_SUITE_STORAGE_SERVER) == VER_SUITE_STORAGE_SERVER) { // Windows Server 2003 Storage Edition return " Storage Edition"; } if ((osVersionInfo.wSuiteMask & VER_SUITE_COMPUTE_SERV) == VER_SUITE_COMPUTE_SERV) { // Windows Server 2003 Compute Cluster Edition return " Compute Cluster Edition"; } if ((osVersionInfo.wSuiteMask & VER_SUITE_BLADE) == VER_SUITE_BLADE) { // Windows Server 2003 Web Edition return " Web Edition"; } // Windows Server 2003 Standard Edition return " Standard Edition"; } } break; case 6: int strProductType; GetProductInfo(osVersionInfo.dwMajorVersion, osVersionInfo.dwMinorVersion, 0, 0, out strProductType); switch (strProductType) { case PRODUCT_ULTIMATE: case PRODUCT_ULTIMATE_E: case PRODUCT_ULTIMATE_N: return "Ultimate Edition"; case PRODUCT_PROFESSIONAL: case PRODUCT_PROFESSIONAL_E: case PRODUCT_PROFESSIONAL_N: return "Professional"; case PRODUCT_HOME_PREMIUM: case PRODUCT_HOME_PREMIUM_E: case PRODUCT_HOME_PREMIUM_N: return "Home Premium Edition"; case PRODUCT_HOME_BASIC: case PRODUCT_HOME_BASIC_E: case PRODUCT_HOME_BASIC_N: return "Home Basic Edition"; case PRODUCT_ENTERPRISE: case PRODUCT_ENTERPRISE_E: case PRODUCT_ENTERPRISE_N: case PRODUCT_ENTERPRISE_SERVER_V: return "Enterprise Edition"; case PRODUCT_BUSINESS: case PRODUCT_BUSINESS_N: return "Business Edition"; case PRODUCT_STARTER: case PRODUCT_STARTER_E: case PRODUCT_STARTER_N: return "Starter Edition"; case PRODUCT_CLUSTER_SERVER: return "Cluster Server Edition"; case PRODUCT_DATACENTER_SERVER: case PRODUCT_DATACENTER_SERVER_V: return "Datacenter Edition"; case PRODUCT_DATACENTER_SERVER_CORE: case PRODUCT_DATACENTER_SERVER_CORE_V: return "Datacenter Edition (core installation)"; case PRODUCT_ENTERPRISE_SERVER: return "Enterprise Edition"; case PRODUCT_ENTERPRISE_SERVER_CORE: case PRODUCT_ENTERPRISE_SERVER_CORE_V: return "Enterprise Edition (core installation)"; case PRODUCT_ENTERPRISE_SERVER_IA64: return "Enterprise Edition for Itanium-based Systems"; case PRODUCT_SMALLBUSINESS_SERVER: return "Small Business Server"; //case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM: // return "Small Business Server Premium Edition"; case PRODUCT_SERVER_FOR_SMALLBUSINESS: case PRODUCT_SERVER_FOR_SMALLBUSINESS_V: return "Windows Essential Server Solutions"; case PRODUCT_STANDARD_SERVER: case PRODUCT_STANDARD_SERVER_V: return "Standard Edition"; case PRODUCT_STANDARD_SERVER_CORE: case PRODUCT_STANDARD_SERVER_CORE_V: return "Standard Edition (core installation)"; case PRODUCT_WEB_SERVER: case PRODUCT_WEB_SERVER_CORE: return "Web Server Edition"; case PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT: case PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING: case PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY: return "Windows Essential Business Server "; case PRODUCT_STORAGE_ENTERPRISE_SERVER: case PRODUCT_STORAGE_EXPRESS_SERVER: case PRODUCT_STORAGE_STANDARD_SERVER: case PRODUCT_STORAGE_WORKGROUP_SERVER: return "Storage Server"; } break; } return string.Empty; }
public static extern bool VerifyVersionInfo(ref OSVERSIONINFOEX versionInfo, int typeMask, ulong conditionMask);
/// <summary> /// Gets the name of the Windows version /// </summary> /// <param name="version">The version.</param> /// <remarks>Looked at http://www.csharp411.com/determine-windows-version-and-edition-with-c/</remarks> private static string get_windows_name(Version version) { var name = "Windows"; var isServer = false; var osVersionInfo = new OSVERSIONINFOEX(); osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof (OSVERSIONINFOEX)); var success = GetVersionEx(ref osVersionInfo); if (success) { isServer = osVersionInfo.wProductType == ServerNT; } //https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832.aspx //switch doesn't like a double, but a string is fine?! string majorMinor = version.Major + "." + version.Minor; switch (majorMinor) { case "6.4": name = isServer ? "Windows Server 2016 (?)" : "Windows 10"; break; case "6.3": name = isServer ? "Windows Server 2012 R2" : "Windows 8.1"; break; case "6.2": name = isServer ? "Windows Server 2012" : "Windows 8"; break; case "6.1": name = isServer ? "Windows Server 2008 R2" : "Windows 7"; break; case "6.0": name = isServer ? "Windows Server 2008" : "Windows Vista"; break; case "5.2": name = isServer ? "Windows Server 2003" : "Windows XP"; break; case "5.1": name = "Windows XP"; break; case "5.0": name = "Windows 2000"; break; } return name; }
internal static extern bool GetVersionEx(ref OSVERSIONINFOEX osVersionInfo);
/// <summary> /// Determine OS version /// </summary> /// <returns></returns> public static WindowsVersion GetVersion() { WindowsVersion ret = WindowsVersion.Unknown; OSVERSIONINFOEX info = new OSVERSIONINFOEX(); info.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)); GetVersionEx(ref info); // Get OperatingSystem information from the system namespace. System.OperatingSystem osInfo = System.Environment.OSVersion; // Determine the platform. switch (osInfo.Platform) { // Platform is Windows 95, Windows 98, Windows 98 Second Edition, or Windows Me. case System.PlatformID.Win32Windows: switch (osInfo.Version.Minor) { case 0: ret = WindowsVersion.Windows95; break; case 10: ret = WindowsVersion.Windows98; break; case 90: ret = WindowsVersion.WindowsMe; break; } break; // Platform is Windows NT 3.51, Windows NT 4.0, Windows 2000, or Windows XP. case System.PlatformID.Win32NT: switch (osInfo.Version.Major) { case 3: ret = WindowsVersion.WindowsNT351; break; case 4: ret = WindowsVersion.WindowsNT4; break; case 5: switch (osInfo.Version.Minor) { case 0: ret = WindowsVersion.Windows2000; break; case 1: ret = WindowsVersion.WindowsXP; break; case 2: int i = GetSystemMetrics(SM_SERVERR2); if (i != 0) { //Server 2003 R2 ret = WindowsVersion.WindowsServer2003; } else { if (info.wProductType == (byte)WinPlatform.VER_NT_WORKSTATION) { //XP Pro x64 ret = WindowsVersion.WindowsXP; } else { ret = WindowsVersion.WindowsServer2003; } break; } break; } break; case 6: switch (osInfo.Version.Minor) { case 0: if (info.wProductType == (byte)WinPlatform.VER_NT_WORKSTATION) ret = WindowsVersion.WindowsVista; else ret = WindowsVersion.WindowsServer2008; break; case 1: if (info.wProductType == (byte)WinPlatform.VER_NT_WORKSTATION) ret = WindowsVersion.Windows7; else ret = WindowsVersion.WindowsServer2008R2; break; case 2: if (info.wProductType == (byte)WinPlatform.VER_NT_WORKSTATION) ret = WindowsVersion.Windows8; else ret = WindowsVersion.WindowsServer2012; break; case 3: ret = WindowsVersion.WindowsServer2012R2; break; default: if (info.wProductType == (byte)WinPlatform.VER_NT_WORKSTATION) ret = WindowsVersion.Win32NTWorkstation; else ret = WindowsVersion.Win32NTServer; break; } break; default: if (info.wProductType == (byte)WinPlatform.VER_NT_WORKSTATION) ret = WindowsVersion.Win32NTWorkstation; else ret = WindowsVersion.Win32NTServer; break; } break; } return ret; }
public static extern bool VerifyVersionInfo(ref OSVERSIONINFOEX lpVersionInfo, VersionTypeMask dwTypeMask, ulong dwlConditionMask);
private static extern bool GetVersionEx(ref OSVERSIONINFOEX versionInfo);
/// <summary> /// Returns the service pack information of the operating system running on this computer. /// </summary> /// <returns>A string containing the the operating system service pack information.</returns> public static string GetOSServicePack() { OSVERSIONINFOEX versionInfo = new OSVERSIONINFOEX(); versionInfo.VersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)); if (!GetVersionEx(ref versionInfo)) { return string.Empty; } else { return " " + versionInfo.ServicePackVersion; } }
/// <summary> /// Returns the product type of the operating system running on this computer. /// </summary> /// <returns>A string containing the the operating system product type.</returns> public static string GetOSProductType() { OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX(); OperatingSystem osInfo = Environment.OSVersion; osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)); if (!GetVersionEx(ref osVersionInfo)) { return ""; } else { if (osInfo.Version.Major == 4) // Windows NT { if (osVersionInfo.wProductType == VER_NT_WORKSTATION) { // Windows NT 4.0 Workstation return "Workstation"; } else if (osVersionInfo.wProductType == VER_NT_SERVER) { // Windows NT 4.0 Server return "Server"; } else { return ""; } } else if (osInfo.Version.Major >= 5) // Windows 2000 or later { if (osVersionInfo.wProductType == VER_NT_WORKSTATION) { if ((osVersionInfo.wSuiteMask & VER_SUITE_PERSONAL) == VER_SUITE_PERSONAL) { // Windows XP Home Edition return "Home Edition"; } else { // Windows XP / Windows 2000 Professional return "Professional"; } } else // Server or Domain Controller { if ((osVersionInfo.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER) { // Windows Server 2003 Datacenter Edition return "Datacenter Edition"; } else if ((osVersionInfo.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE) { // Windows Server 2003 Enterprise Edition return "Enterprise Edition"; } else if ((osVersionInfo.wSuiteMask & VER_SUITE_BLADE) == VER_SUITE_BLADE) { // Windows Server 2003 Web Edition return "Web Edition"; } else if ((osVersionInfo.wSuiteMask & VER_SUITE_COMPUTE_SERVER) == VER_SUITE_COMPUTE_SERVER) { // Windows Server 2003 Web Edition return "Compute Cluster Edition"; } else if ((osVersionInfo.wSuiteMask & VER_SUITE_STORAGE_SERVER) == VER_SUITE_STORAGE_SERVER) { // Windows Server 2003 Web Edition return "Storage Server Edition"; } else if ((osVersionInfo.wSuiteMask & VER_SUITE_WH_SERVER) == VER_SUITE_WH_SERVER) { // Windows Server 2003 Web Edition return "Home Server Edition"; } else { // Windows Server 2003 Standard Edition return "Standard Edition"; } } } } return ""; }
/// <summary> /// Returns the service pack information of the operating system running on this computer. /// </summary> /// <returns>A string containing the the operating system service pack information.</returns> public static string GetOSServicePack() { OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX(); osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)); if (!GetVersionEx(ref osVersionInfo)) { return ""; } else { return " " + osVersionInfo.szCSDVersion; } }
public static extern bool GetVersionEx(ref OSVERSIONINFOEX lpVersionInfo);
/// <summary> /// Returns the service pack information of the operating system running on this computer. /// </summary> /// <returns>A string containing the the operating system service pack information.</returns> public static string GetOSServicePack() { OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX(); osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof (OSVERSIONINFOEX)); return !GetVersionEx(ref osVersionInfo) ? string.Empty : osVersionInfo.szCSDVersion; }
public static WINDOWS_OS? GetOS() { OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX(); OperatingSystem osInfo = Environment.OSVersion; osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)); if (!GetVersionEx(ref osVersionInfo)) { return null; } else { if (osInfo.Version.Major == 5) { if (osVersionInfo.wProductType == VER_NT_WORKSTATION) { if (osInfo.Version.Minor == 0) { return WINDOWS_OS.Windows2000; } else { return WINDOWS_OS.WindowsXP; } } else if (osVersionInfo.wProductType == VER_NT_SERVER) { if (osInfo.Version.Minor == 0) { return WINDOWS_OS.Windows2000Server; } else if (osInfo.Version.Minor == 2) { return WINDOWS_OS.WindowsServer2003; } } } else if (osInfo.Version.Major == 6) { if (osVersionInfo.wProductType == VER_NT_WORKSTATION) { if (osInfo.Version.Minor == 0) { return WINDOWS_OS.WindowsVista; } else if (osInfo.Version.Minor == 1) { return WINDOWS_OS.Windows7; } } else if (osVersionInfo.wProductType == VER_NT_SERVER) { if (osInfo.Version.Minor == 0) { return WINDOWS_OS.WindowsServer2008; } else if (osInfo.Version.Minor == 1) { return WINDOWS_OS.WindowsServer2008R2; } } } } return null; }
/// <summary> /// Checks if OS is later then major / minor version /// </summary> /// <param name="majorVersion">Major OS version</param> /// <param name="minorVersion">Minor OS version</param> /// <returns>True if OS is later than version supplied as parameters</returns> private static bool VerifyVersionGreaterEqual(int majorVersion, int minorVersion) { ulong condition = 0; var osVersionInfo = new OSVERSIONINFOEX { dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)), dwMajorVersion = majorVersion, dwMinorVersion = minorVersion }; condition = VerSetConditionMask(condition, VER_MAJORVERSION, VER_GREATER_EQUAL); condition = VerSetConditionMask(condition, VER_MINORVERSION, VER_GREATER_EQUAL); return VerifyVersionInfo(ref osVersionInfo, VER_MAJORVERSION | VER_MINORVERSION, condition); }
static private extern Boolean GetVersionEx(ref OSVERSIONINFOEX osVersionInfo);
/// <summary> /// Identifies if OS is a Windows Server OS /// </summary> /// <returns>True if OS is a Windows Server OS</returns> private static bool IsServer() { ulong condition = 0; var osVersionInfo = new OSVERSIONINFOEX { dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)), wProductType = NT_WORKSTATION // note the check is that this is not equal as per MS documenation }; condition = VerSetConditionMask(condition, VER_PRODUCT_TYPE, VER_EQUAL); return !VerifyVersionInfo(ref osVersionInfo, VER_PRODUCT_TYPE, condition); }
private void InitOsVersionInfoEx() { OSVERSIONINFOEX info = new OSVERSIONINFOEX(); bool b = NativeMethods.GetVersionEx( info ); if ( ! b ) { int error = Marshal.GetLastWin32Error(); throw new InvalidOperationException( "Failed to get OSVersionInfoEx. Error = 0x" + error.ToString( "8X", CultureInfo.CurrentCulture ) ); } OSPlatformId = GetOSPlatformId( info.PlatformId ); OSMajorVersion = info.MajorVersion ; OSMinorVersion = info.MinorVersion ; BuildNumber = info.BuildNumber ; // PlatformId = info.PlatformId ; OSCSDVersion = info.CSDVersion ; OSSuiteFlags = GetOSSuiteFlags ( info.SuiteMask ); OSProductType = GetOSProductType( info.ProductType ); OSServicePackMajor = info.ServicePackMajor ; OSServicePackMinor = info.ServicePackMinor ; // SuiteMask = info.SuiteMask ; // ProductType = info.ProductType ; OSReserved = info.Reserved ; ExtendedPropertiesAreSet = true; }
private static extern bool GetVersionEx( ref OSVERSIONINFOEX osvi );
private void GetOSInfo() { OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX(); osVersionInfo.dwOSVersionInfoSize = (uint)Marshal.SizeOf(typeof(OSVERSIONINFOEX)); if (!GetVersionEx(ref osVersionInfo)) { this._version = "Unknown"; this._servicePack = 0; return; } string osName = ""; SYSTEM_INFO systemInfo = new SYSTEM_INFO(); GetSystemInfo(ref systemInfo); switch (Environment.OSVersion.Platform) { case PlatformID.Win32Windows: { switch (osVersionInfo.dwMajorVersion) { case 4: { switch (osVersionInfo.dwMinorVersion) { case 0: if (osVersionInfo.szCSDVersion == "B" || osVersionInfo.szCSDVersion == "C") osName += "Windows 95 R2"; else osName += "Windows 95"; break; case 10: if (osVersionInfo.szCSDVersion == "A") osName += "Windows 98 SE"; else osName += "Windows 98"; break; case 90: osName += "Windows ME"; break; } } break; } } break; case PlatformID.Win32NT: { switch (osVersionInfo.dwMajorVersion) { case 3: osName += "Windows NT 3.5.1"; break; case 4: osName += "Windows NT 4.0"; break; case 5: { switch (osVersionInfo.dwMinorVersion) { case 0: osName += "Windows 2000"; break; case 1: osName += "Windows XP"; break; case 2: { if (osVersionInfo.wSuiteMask == VER_SUITE_WH_SERVER) osName += "Windows Home Server"; else if (osVersionInfo.wProductType == VER_NT_WORKSTATION && systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) osName += "Windows XP"; else osName += GetSystemMetrics(SM_SERVERR2) == 0 ? "Windows Server 2003" : "Windows Server 2003 R2"; } break; } } break; case 6: { switch (osVersionInfo.dwMinorVersion) { case 0: osName += osVersionInfo.wProductType == VER_NT_WORKSTATION ? "Windows Vista" : "Windows Server 2008"; break; case 1: osName += osVersionInfo.wProductType == VER_NT_WORKSTATION ? "Windows 7" : "Windows Server 2008 R2"; break; case 2: osName += osVersionInfo.wProductType == VER_NT_WORKSTATION ? "Windows 8" : "Windows Server 8"; break; } } break; } } break; } this._version = osName; this._servicePack = osVersionInfo.wServicePackMajor; return; }
private static extern int GetVersionEx(ref OSVERSIONINFOEX lpVersionInformation);
/// <summary> /// Indicates if the current OS version matches, or is greater than, the provided version /// information. This method is useful in confirming a version of Windows Server that /// doesn't share a version number with a client release. /// </summary> /// <remarks> /// You should only use this method if the other provided version helper methods do not fit /// your scenario. /// </remarks> /// <exception cref="System.ArgumentOutOfRangeException"> /// <paramref name="majorVersion"/> is smaller than 5 or <paramref name="minorVersion"/> /// and/or <paramref name="servicePackMajor"/> are smaller than 0. /// </exception> /// <param name="majorVersion">The major OS version number.</param> /// <param name="minorVersion">The minor OS version number.</param> /// <param name="servicePackMajor">The major Service Pack version number.</param> /// <returns>True if the specified version matches, or is greater than, the version of the /// current Windows OS; otherwise, false.</returns> public static Boolean IsWindowsVersionOrGreater(Int32 majorVersion, Int32 minorVersion, Int32 servicePackMajor) { // Validate arguments if (majorVersion < 5) { // Error: Major version cannot be smaller than 5 throw new ArgumentOutOfRangeException("majorVersion"); } if (minorVersion < 0) { // Error: Minor version cannot be negative throw new ArgumentOutOfRangeException("minorVersion"); } if (servicePackMajor < 0) { // Error: Major service pack version cannot be negative throw new ArgumentOutOfRangeException("servicePackMajor"); } // Initialize OSVERSIONINFOEX structure OSVERSIONINFOEX osvi = new OSVERSIONINFOEX(); osvi.dwOSVersionInfoSize = (UInt32)Marshal.SizeOf(typeof(OSVERSIONINFOEX)); osvi.dwMajorVersion = (UInt32)majorVersion; osvi.dwMinorVersion = (UInt32)minorVersion; osvi.wServicePackMajor = (UInt16)servicePackMajor; // Initialize condition mask UInt64 conditionMask = VerSetConditionMask( VerSetConditionMask( VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL), VER_MINORVERSION, VER_GREATER_EQUAL), VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL); // Verify version info return VerifyVersionInfo(ref osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, conditionMask); }
public static extern bool GetVersionExW(ref OSVERSIONINFOEX osvi);
internal static Boolean IsWindowsServerInternal() { // Initialize OSVERSIONINFOEX structure OSVERSIONINFOEX osvi = new OSVERSIONINFOEX(); osvi.dwOSVersionInfoSize = (UInt32)Marshal.SizeOf(typeof(OSVERSIONINFOEX)); osvi.wProductType = VER_NT_WORKSTATION; // Initialize condition mask UInt64 conditionMask = VerSetConditionMask(0, VER_PRODUCT_TYPE, VER_EQUAL); // Verify version info return !VerifyVersionInfo(ref osvi, VER_PRODUCT_TYPE, conditionMask); }