【问题标题】:C# - Detect if is the user using Windows 7 or Windows 8 [duplicate]C# - 检测用户是使用 Windows 7 还是 Windows 8 [重复]
【发布时间】:2014-07-14 12:12:18
【问题描述】:

我在检测用户正在运行的操作系统时遇到了问题。我可以检测到它是否是版本 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
            {

            }
        }
    }
}

但这不起作用。

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

【问题讨论】:

  • System.Environment.OSVersion.Version.Minor
  • 但它只检测到像 1 或 3 这样的值,它仍然没有检测到它是否是 Windows 6

标签: c# windows


【解决方案1】:
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();

【讨论】:

  • @user3669879 你能具体告诉我什么无效吗?是抛出异常还是没有正确检测到操作系统?
  • 我认为它没有正确检测操作系统,因为程序只是退出而没有显示任何文本。
  • 在末尾添加Console.ReadLine();
  • 我第一次加了,还是不行。
  • 我改了代码试试看。
猜你喜欢
  • 1970-01-01
  • 2012-12-26
  • 1970-01-01
  • 2020-02-06
  • 1970-01-01
  • 1970-01-01
  • 2011-11-10
  • 1970-01-01
  • 2014-07-04
相关资源
最近更新 更多