1

What is the correct way to determine the correct OS version in .NET 6?

I found many different solutions regarding this topic. All of them are quite out of date I think.

I want to get something like "Windows 10 Enterprise build 22000 (64 bit)"

Environment.OSVersion.ToString()

gives "Microsoft Windows NT 10.0.22000.0"

RuntimeInformation.OSDescription 

gives "Microsoft Windows 10.0.22000.0"

RuntimeInformation.OSArchitecture.ToString() 

gives "X64"

So far I'm using:

Console.WriteLine(RuntimeInformation.OSDescription + " | " + RuntimeInformation.OSArchitecture.ToString())

This gives "Microsoft Windows 10.0.22000.0 | X64"

Is there a way to get something like "Windows 10 Enterprise | X64" in .NET 6?

In addition to that, I'm looking for a way to get the Windows install language and the current language.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
KUMI
  • 39
  • 5
  • Can you please clarify what is the problem with code shown in the post? – Alexei Levenkov Feb 06 '23 at 22:54
  • Does this answer your question? [Get Windows Edition](https://stackoverflow.com/questions/4405761/get-windows-edition) – gunr2171 Feb 06 '23 at 23:11
  • Since .net6 can be used for cross-plattform applications, the answer of your mentioned question are not working. That's the reason I'm asking, because most of the anwers I found are like 10 years old. – KUMI Feb 06 '23 at 23:17
  • Please [edit] the question to clarify that. I.e. "I need Windows Edition name of the current OS running .Net6 code on Linux, the suggested duplicate {link} works only for showing editions of Windows running on Windows" (or something like that). – Alexei Levenkov Feb 07 '23 at 00:18
  • Also note that if you care someone to see your answer to the comment you should @ - refer them like @KUMI... Also proper comments do not need replies but rather edits to the post (as comments are for asking clarification for questions/answers rather than starting discussion). – Alexei Levenkov Feb 07 '23 at 00:22

1 Answers1

1

The function:

RuntimeInformation.OSArchitecture.ToString();

returns the architecture that the OS was compiled, that is, in this case it was x86-64 (string-shaped)

The functions:

Environment.OSVersion.ToString()
RuntimeInformation.OSDescription

returns windows version

With this and with this link here: (Get OS Version / Friendly Name in C#) we can get to this code:

using System.Runtime.InteropServices;
using System.Management;

static int GetARCHFriendlyBits(Architecture architecture)
{
    return architecture switch
    {
        Architecture.X64 => 64,
        Architecture.X86 => 32,
        Architecture.Arm64 => 64,
        Architecture.Arm => 32,
        Architecture.Wasm => -1,
        Architecture.S390x => -1,
        _ => -1,
    };
}

static string GetOSFriendlyName1()
{
    string result = string.Empty;
    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
    {
        ManagementObjectSearcher searcher = new("SELECT Caption FROM Win32_OperatingSystem");
        ManagementObject os = searcher.Get().Cast<ManagementObject>().First();
        if (os["Caption"].ToString() is string osResult)
            result = osResult;
    }
    else
    {
        return $"{RuntimeInformation.OSDescription} ({RuntimeInformation.OSArchitecture})";
    }

    if (result == string.Empty)
        return $"{RuntimeInformation.OSDescription} ({RuntimeInformation.OSArchitecture})";
    else
        return $"{result} build {Environment.OSVersion.Version.Build} ({GetARCHFriendlyBits(RuntimeInformation.OSArchitecture)} bits)";
}

static string GetOSFriendlyName2()
{
    string result = string.Empty;
    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
    {
        ManagementObjectSearcher searcher = new("SELECT Caption FROM Win32_OperatingSystem");
        ManagementObject os = searcher.Get().Cast<ManagementObject>().First();
        if (os["Caption"].ToString() is string osResult)
            result = osResult;
    }
    else
    {
        return $"{RuntimeInformation.OSDescription} ({RuntimeInformation.OSArchitecture})";
    }

    if (result == string.Empty)
        return $"{RuntimeInformation.OSDescription} ({RuntimeInformation.OSArchitecture})";
    else
        return $"{result} | {RuntimeInformation.OSArchitecture}";
}

Console.WriteLine(GetOSFriendlyName1());
Console.WriteLine(GetOSFriendlyName2());

which in my case writes this line here on the console:

Microsoft Windows 11 Home Single Language build 22621 (64 bits)
Microsoft Windows 11 Home Single Language | X64

To use System.Management, I had to install microsoft NuGet System.Management