C# 判别系统版本以及Win10的识别办法

我是一名 ASP.NET 程序员,专注于 B/S 项目开发。累计文章阅读量超过一千万,我的博客主页地址:https://www.itsvse.com/blog_xzz.html

 

我们都知道在C#中可以通过Environment.OSVersion来判断当前操作系统,下面是操作系统和主次版本的对应关系:

操作系统
主版本.次版本
Windows 10 10.0*
Windows Server 2016 Technical Preview 10.0*
Windows 8.1 6.3*
Windows Server 2012 R2 6.3*
Windows 8 6.2
Windows Server 2012 6.2
Windows 7 6.1
Windows Server 2008 R2 6.1
Windows Server 2008 6
Windows Vista 6
Windows Server 2003 R2 5.2
Windows Server 2003 5.2
Windows XP 64-Bit Edition 5.2
Windows XP 5.1
Windows 2000 5


我们可以用Environment.OSVersion来判断当前操作系统

public static bool IsWin7 => Environment.OSVersion.Version.Major == 6
                       && Environment.OSVersion.Version.Minor == 1;
public static bool IsWin10 => Environment.OSVersion.Version.Major == 10;

 

 

但是,当你在win10操作系统上使用这个函数是会得到这样的结果:6.2.9200.0,而不是我们期待的10.0.***

问题是Win10下获取的值可能不是10,说明地址: https://docs.microsoft.com/zh-cn/windows/win32/sysinfo/operating-system-version

For applications that have been manifested for Windows 8.1 or Windows 10. Applications not manifested for Windows 8.1 or Windows 10 will return the Windows 8 OS version value (6.2). To manifest your applications for Windows 8.1 or Windows 10, refer to Targeting your application for Windows.

现在需要一个程序清单文件



然后把下面的注释去掉,就可以返回10.0.***了





还有另外一种方法如下。

利用C#判断当前操作系统是否为Win8系统(此方法不需要添加程序清单文件)

代码:

using System;
  
namespace GetOSVersionExp
{
  class Program
  {
    static void Main(string[] args)
    {
      Version currentVersion = Environment.OSVersion.Version;
      Version compareToVersion = new Version("6.2");
      if (currentVersion.CompareTo(compareToVersion) >= 0)
      {//win8及其以上版本的系统
        Console.WriteLine("当前系统是WIN8及以上版本系统。");
      }
      else
      {
        Console.WriteLine("当前系统不是WIN8及以上版本系统。");
      }
    }
  }
}

 

(完)

posted @ 2019-10-23 13:20  _小渣渣  阅读(3456)  评论(1编辑  收藏  举报