Reputation: 49
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.
Upvotes: 2
Views: 1874
Reputation: 326
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
Upvotes: 2