C# - 检测用户是否使用Windows 7或Windows 8

时间:2014-05-25 17:18:12

标签: c# windows

我在检测用户正在运行的操作系统时遇到问题。我可以检测到它是否是版本6但我无法检测6.1或6.2 到目前为止,这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OSDetect
{
    class Program
    {
        static void Main(string[] args)
        {
            string osVer = System.Environment.OSVersion.Version.ToString();

            if (osVer.StartsWith("6.1"))
            {
                Console.WriteLine("This program isn't compatible with Windows 7 and older.");
                Console.ReadKey();
            }
            else
            {

            }
            if (osVer.StartsWith("6.2"))
            {
                Console.Write("> ");
                Console.ReadKey();
            }
            else
            {

            }
        }
    }
}

但这不起作用。

所以基本上我希望它也能检测到次要版本。我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

var version = Environment.OSVersion.Version;

if (version < new Version(6, 2))
{
    Console.WriteLine("This program isn't compatible with Windows 7 and older.");
}
else
{
    Console.WriteLine("This os is compatible");
}

Console.ReadLine();