2. 展示

2.1 传统获取方式（弃用）

public static string get_OSVersion()
{
//Get Operating system information.
OperatingSystem os = Environment.OSVersion;
//Get version information about the os.
Version vs = os.Version;

//Variable to hold our return value
string operatingSystem = "";

if (os.Platform == PlatformID.Win32Windows)
{
//This is a pre-NT version of Windows
switch (vs.Minor)
{
case 0:
operatingSystem = "95";
break;
case 10:
if (vs.Revision.ToString() == "2222A")
operatingSystem = "98SE";
else
operatingSystem = "98";
break;
case 90:
operatingSystem = "Me";
break;
default:
break;
}
}
else if (os.Platform == PlatformID.Win32NT)
{
switch (vs.Major)
{
case 3:
operatingSystem = "NT 3.51";
break;
case 4:
operatingSystem = "NT 4.0";
break;
case 5:
if (vs.Minor == 0)
operatingSystem = "2000";
else
operatingSystem = "XP";
break;
case 6:
if (vs.Minor == 0)
operatingSystem = "Vista";
else if (vs.Minor == 1)
operatingSystem = "7";
else if (vs.Minor == 2)
operatingSystem = "8";
else
operatingSystem = "8.1";
break;
case 10:
operatingSystem = "10";
break;
default:
break;
}
}
//Make sure we actually got something in our OS check
//We don't want to just return " Service Pack 2" or " 32-bit"
//That information is useless without the OS version.
if (operatingSystem != "")
{
operatingSystem = "Windows " + operatingSystem;
//See if there's a service pack installed.
if (os.ServicePack != "")
{
//Append it to the OS name.  i.e. "Windows XP Service Pack 3"
operatingSystem += " " + os.ServicePack;
}
//Append the OS architecture.  i.e. "Windows XP Service Pack 3 32-bit"
//operatingSystem += " " + getOSArchitecture().ToString() + "-bit";
}
//Return the information we've gathered.
return operatingSystem;
}


3.2 通过 WMI 获取操作系统版本

public static string get_OSVersion()
{
try
{
ManagementObject mo = GetMngObj("Win32_OperatingSystem");

if (null == mo)
return string.Empty;

return mo["Version"] as string;
}
catch (Exception e)
{
return string.Empty;
}
}


3.3 直接获取操作系统名称

+------------------------------------------------------------------------------------+
|                          |   PlatformID    |   Major version   |   Minor version   |
+------------------------------------------------------------------------------------+
| Windows 95               |  Win32Windows   |         4         |          0        |
| Windows 98               |  Win32Windows   |         4         |         10        |
| Windows Me               |  Win32Windows   |         4         |         90        |
| Windows NT 4.0           |  Win32NT        |         4         |          0        |
| Windows 2000             |  Win32NT        |         5         |          0        |
| Windows XP               |  Win32NT        |         5         |          1        |
| Windows XP 64-Bit Edition|  Win32NT        |         5         |          1        |
| Windows 2003             |  Win32NT        |         5         |          2        |
| Windows Server 2003      |  Win32NT        |         5         |          2        |
| Windows Server 2003 R2   |  Win32NT        |         5         |          2        |
| Windows 2003             |  Win32NT        |         5         |          2        |
| Windows Vista            |  Win32NT        |         6         |          0        |
| Windows 2008             |  Win32NT        |         6         |          0        |
| Windows Server 2008      |  Win32NT        |         6         |          0        |
| Windows 7                |  Win32NT        |         6         |          1        |
| Windows 2008 R2          |  Win32NT        |         6         |          1        |
| Windows Server 2008 R2   |  Win32NT        |         6         |          1        |
| Windows 8                |  Win32NT        |         6         |          2        |
| Windows Server 2012      |  Win32NT        |         6         |          2        |
| Windows 8.1              |  Win32NT        |         6         |          3        |
| Windows Server 2012 R2   |  Win32NT        |         6         |          3        |
+------------------------------------------------------------------------------------+
| Windows Server 2016 Technical Preview|  Win32NT| 10            |          0        |
| Windows 10               |  Win32NT        |        10         |          0        |
+------------------------------------------------------------------------------------+


 new ComputerInfo().OSFullName