简体   繁体   English

如何在 .NET 5.0 控制台应用程序的 C# 中检查 windows 版本

[英]How to check windows version in C# on .NET 5.0 console app

Like im working on a "console game engine" but it only works on Windows 10 since "custom colors" arent properly displayed on other versions, for example win8, instead they are just a bunch of unicode codes?就像我在“控制台游戏引擎”上工作一样,但它只适用于 Windows 10,因为“自定义颜色”不能在其他版本(例如 win8)上正确显示,而是它们只是一堆 unicode 代码? " "

例子.png " "

so i wanna check if windows version is 10. and i did this所以我想检查 windows 版本是否为 10。我做到了

var workingSysVer = Environment.OSVersion.Version.ToString().Substring(0, 2);
            if (workingSysVer != "10")
            {
                Console.Title = "OS Version Warning";
                // other code
            }

Is there a better way to check version of windows?有没有更好的方法来检查 windows 的版本?

You can use what you need from this (in addition to OSVersion you can use osName and osRelease ):你可以使用你需要的东西(除了OSVersion你可以使用osNameosRelease ):

using System;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using Microsoft.Win32;

string NL = Environment.NewLine;
string HKLMWinNTCurrent = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion";

string osName = get(() => Registry.GetValue(HKLMWinNTCurrent, "productName", "").ToString());

string osRelease = get(() => Registry.GetValue(HKLMWinNTCurrent, "ReleaseId", "").ToString());
if ( !osRelease.IsNullOrEmpty() ) osRelease = $" ({ osRelease})";

string osVersion = Environment.OSVersion.Version.ToString();

string osType = Environment.Is64BitOperatingSystem ? "64-bits" : "32-bits";

string clr = Environment.Version.ToString();

string dotnet = get(() =>
{
  var attributes = Assembly.GetExecutingAssembly().CustomAttributes;
  var result = attributes.FirstOrDefault(a => a.AttributeType == typeof(TargetFrameworkAttribute));
  return result == null
          ? ".NET Framework (unknown)"
          : result.NamedArguments[0].TypedValue.Value.ToString();
});

string Platform = $"{osName} {osType} {osVersion}{osRelease}{NL}{dotnet}{NL}CLR {clr}";

string get(Func<string> func)
{
  try { return func(); }
  catch { return "(undefined)"; }
}

Example例子

Windows 10 Pro 64-bits 6.2.9200.0 (2009)
.NET Framework 4.7.2
CLR 4.0.30319.42000

I never noticed that 6.2.9200 is incorrect ... ??我从来没有注意到 6.2.9200 是不正确的......??

Solution from @nap : How to get Windows Version - as in "Windows 10, version 1607"? @nap 的解决方案如何获得 Windows 版本 - 如“Windows 10,版本 1607”?

Explanation from @DanielDiPaolo : Detect Windows version in .net @DanielDiPaolo 的解释在 .net 中检测 Windows 版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c# omnisharp intellisense 不适用于 vscode 中的 net 5.0(Windows 10) - c# omnisharp intellisense not working with net 5.0 in vscode (windows 10) 如何在C#中为带有.NET 2.0的Windows CE 5.0应用程序使用AddFontResource - How to use AddFontResource in c# for Windows CE 5.0 application with .NET 2.0 如何检查注册号是否包含字符串-C#控制台应用 - How to check if registration number includes string - c# console app Windows Embedded CE 5.0移动应用程序gridview C# - Windows Embedded CE 5.0 mobile app gridview C# 如何在c#console(windows form)app中使用WinRT组件? - How to use a WinRT component in c# console(windows form) app? 如何在C#控制台应用程序中输出Windows Alt键码 - How to output windows Alt keycodes in a C# console app 如何以 windows 形式可视化来自 c# 控制台应用程序的数据? - How to visualize the data from c# console app in windows form? 如何在 C# Windows 控制台应用程序中执行命令? - How to execute command in a C# Windows console app? C#Windows Form .Net和DOS控制台 - C# Windows Form .Net and DOS Console 如何在C#控制台应用程序.NET 4.5中实现并发调用? - How to implement concurrent calls in C# console app, .NET 4.5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM