我们通过System.Environment.OSVersion.Version获得操作系统的版本号,然后再根据版本号进行判断操作系统是什么版本
Version 类的属性
Operating system | Version number | dwMajorVersion | dwMinorVersion |
Windows 10 | 10.0 | 10 | 0 |
Windows 8.1 | 6.3 | 6 | 3 |
Windows 8 | 6.2 | 6 | 2 |
Windows 7 | 6.1 | 6 | 1 |
Windows Server 2008 | 6.0 | 6 | 0 |
Windows Server 2003 | 5.2 | 5 | 2 |
Windows 2000 | 5.0 | 5 | 0 |
参考于:https://msdn.microsoft.com/zh-cn/library/windows/desktop/ms724834(v=vs.85).aspx
注意:在msdn官方文档有说明 → OSVersion 属性报告两个相同的版本号 (6.2.0.0) Windows 8 和 Windows 8.1。在某些情况下, OSVersion 属性可能不会返回与指定的 Windows 程序兼容性模式功能的版本相匹配的操作系统版本。
代码:
public class GetOSystem
{
private const string Windows2000 = "5.0";
private const string WindowsXP = "5.1";
private const string Windows2003 = "5.2";
private const string Windows2008 = "6.0";
private const string Windows7 = "6.1";
private const string Windows8OrWindows81 = "6.2";
private const string Windows10 = "10.0"; private string OSystemName; public void setOSystemName(string oSystemName)
{
this.OSystemName = oSystemName;
} public GetOSystem()
{
switch (System.Environment.OSVersion.Version.Major + "." + System.Environment.OSVersion.Version.Minor)
{
case Windows2000:
setOSystemName("Windows2000");
break;
case WindowsXP:
setOSystemName("WindowsXP");
break;
case Windows2003:
setOSystemName("Windows2003");
break;
case Windows2008:
setOSystemName("Windows2008");
break;
case Windows7:
setOSystemName("Windows7");
break;
case Windows8OrWindows81:
setOSystemName("Windows8.OrWindows8.1");
break;
case Windows10:
setOSystemName("Windows10");
break;
} Console.WriteLine(OSystemName);
}
}
GetOSyste
class Program
{
static void Main(string[] args)
{
#region 检测系统 new GetOSystem(); #endregion
}
}
输出结果: