[英]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 代码? " "
" "
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
你可以使用osName
和osRelease
):
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.