3

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? "

example.png"

so i wanna check if windows version is 10. and i did this

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?

KSZLAGK
  • 43
  • 4
  • Only Windows or multi-platform ? –  Jan 18 '21 at 20:07
  • The app is windows only, i mean it produces .exe file soo – KSZLAGK Jan 18 '21 at 20:08
  • 1
    Does this answer your question? [How to get Windows Version - as in "Windows 10, version 1607"?](https://stackoverflow.com/questions/39778525/how-to-get-windows-version-as-in-windows-10-version-1607) and [Detect Windows version in .net](https://stackoverflow.com/questions/2819934/detect-windows-version-in-net) –  Jan 18 '21 at 20:36
  • 1
    I mean, i did something like this: if(Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "productName", "").ToString().Substring(0, 10) != "Windows 10") { // other stuff } – KSZLAGK Jan 18 '21 at 20:45
  • @.KSZLAGK You can simplify by using `.StartWith("Windows 10")` to avoid potential `SubString` errors. –  Jan 18 '21 at 20:47
  • 1
    Oh, oh yea that will do – KSZLAGK Jan 18 '21 at 20:50
  • 1
    `.StartWith("Windows 10")`... and this is why Microsoft will have to skip Windows 100 ;) – omajid Jan 18 '21 at 21:45
  • @omajid Indeed, so the same as substring... `.StartWith("Windows 10 ") || == "Windows 10"` ? –  Jan 19 '21 at 06:39

1 Answers1

4

You can use what you need from this (in addition to OSVersion you can use osName and 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... !?

Solution from @nap: How to get Windows Version - as in "Windows 10, version 1607"?

Explanation from @DanielDiPaolo: Detect Windows version in .net