static void EnsureVersionInfo() { if (name != null) { return; } var osVersion = Environment.OSVersion; var osVersionInfo = new OSVERSIONINFOEX { dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)) }; if (Win32API.GetVersionEx(ref osVersionInfo)) { name = GetOSName(osVersion, osVersionInfo) ?? "Unknown"; servicePack = osVersionInfo.szCSDVersion ?? string.Empty; } else { name = "Unknown"; servicePack = string.Empty; } }
public static extern bool GetVersionEx(ref OSVERSIONINFOEX osVersionInfo);
static string GetOSName(OperatingSystem osVersion, OSVERSIONINFOEX osVersionInfo) { var majorVersion = osVersion.Version.Major; var minorVersion = osVersion.Version.Minor; switch (osVersion.Platform) { case PlatformID.Win32S: return "Windows 3.1"; case PlatformID.WinCE: return "Windows CE"; case PlatformID.Win32Windows: { if (majorVersion == 4) { var csdVersion = osVersionInfo.szCSDVersion; switch (minorVersion) { case 0: return csdVersion == "B" || csdVersion == "C" ? "Windows 95 OSR2" : "Windows 95"; case 10: return csdVersion == "A" ? "Windows 98 Second Edition" : "Windows 98"; case 90: return "Windows Me"; } } break; } case PlatformID.Win32NT: { var productType = osVersionInfo.wProductType; switch (majorVersion) { case 3: return "Windows NT 3.51"; case 4: switch (productType) { case 1: return "Windows NT 4.0"; case 3: return "Windows NT 4.0 Server"; } break; case 5: switch (minorVersion) { case 0: return "Windows 2000"; case 1: return "Windows XP"; case 2: return "Windows Server 2003"; } break; case 6: switch (minorVersion) { case 0: switch (productType) { case 1: return "Windows Vista"; case 3: return "Windows Server 2008"; } break; case 1: switch (productType) { case 1: return "Windows 7"; case 3: return "Windows Server 2008 R2"; } break; } break; } break; } } return null; }
static void EnsureVersionInfo() { if (name != null) return; var osVersion = Environment.OSVersion; var osVersionInfo = new OSVERSIONINFOEX { dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)) }; if (Win32API.GetVersionEx(ref osVersionInfo)) { name = GetOSName(osVersion, osVersionInfo) ?? "Unknown"; servicePack = osVersionInfo.szCSDVersion ?? string.Empty; } else { name = "Unknown"; servicePack = string.Empty; } }
static string GetOSName(OperatingSystem osVersion, OSVERSIONINFOEX osVersionInfo) { var majorVersion = osVersion.Version.Major; var minorVersion = osVersion.Version.Minor; switch (osVersion.Platform) { case PlatformID.Win32S: return("Windows 3.1"); case PlatformID.WinCE: return("Windows CE"); case PlatformID.Win32Windows: { if (majorVersion == 4) { var csdVersion = osVersionInfo.szCSDVersion; switch (minorVersion) { case 0: return(csdVersion == "B" || csdVersion == "C" ? "Windows 95 OSR2" : "Windows 95"); case 10: return(csdVersion == "A" ? "Windows 98 Second Edition" : "Windows 98"); case 90: return("Windows Me"); } } break; } case PlatformID.Win32NT: { var productType = osVersionInfo.wProductType; switch (majorVersion) { case 3: return("Windows NT 3.51"); case 4: switch (productType) { case 1: return("Windows NT 4.0"); case 3: return("Windows NT 4.0 Server"); } break; case 5: switch (minorVersion) { case 0: return("Windows 2000"); case 1: return("Windows XP"); case 2: return("Windows Server 2003"); } break; case 6: switch (minorVersion) { case 0: switch (productType) { case 1: return("Windows Vista"); case 3: return("Windows Server 2008"); } break; case 1: switch (productType) { case 1: return("Windows 7"); case 3: return("Windows Server 2008 R2"); } break; } break; } break; } } return(null); }