Files
Crapfixer/MSCFixer/Helpers/OSHelper.cs
T
2025-05-07 19:39:40 +02:00

52 lines
2.1 KiB
C#

using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Threading.Tasks;
using Microsoft.Win32;
// This file is part of MSCFixer.
namespace OSHelper
{
internal class OSHelper
{
public static async Task<string> GetWindowsVersion()
{
return await Task.Run(() =>
{
try
{
using (PowerShell powerShellInstance = PowerShell.Create())
{
powerShellInstance.AddScript("Get-CimInstance -ClassName Win32_OperatingSystem");
Collection<PSObject> psOutput = powerShellInstance.Invoke();
foreach (PSObject outputItem in psOutput)
{
if (outputItem != null)
{
string productName = outputItem.Properties["Caption"]?.Value?.ToString();
if (!string.IsNullOrEmpty(productName))
{
string osVersion = productName.Contains("Windows 10") ? "Windows 10" : "Windows 11";
using (RegistryKey displayVersionKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion"))
{
if (displayVersionKey != null)
{
string displayVersion = displayVersionKey.GetValue("DisplayVersion")?.ToString();
return $"{osVersion} ({displayVersion})";
}
}
return osVersion;
}
}
}
}
}
catch
{
}
return "OS not supported";
});
}
}
}