1

I use .NET 6 to get os version.
I want to get os version like "Windows 2019 Enterprise LTSC" or "Windows 10 Professional".
I use System.Management:

string? info = null;
                using (ManagementObjectSearcher searcher = new("SELECT * FROM Win32_OperatingSystem"))
                {
                    ManagementObjectCollection information = searcher.Get();
                    if (information != null)
                    {
                        foreach (ManagementObject obj in information.Cast<ManagementObject>())
                        {
                            info = obj["Caption"].ToString() + " - " + obj["OSArchitecture"].ToString();
                        }
                    }
                    info = info.Replace("NT 5.1.2600", "XP");
                    info = info.Replace("NT 5.2.3790", "Server 2003");
                }
                return info;

I get os version wonderful,but PublishTrimmed was not allowed:
<PublishTrimmed>true</PublishTrimmed>
System.Management was conflicted with PublishTrimmed.
I tried to use Microsoft.VisualBasic.Device.ComputerInfo,but I need to change .net6.0 to .net6.0-windows and add <UseWindowsForms>true<UseWindowsForms>.When I use this,I can not use <PublishTrimmed>true</PublishTrimmed>.
For some reason,I need reduce my program size less than 20M.
I tried to use registry to get os version:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion
But in Windows 11 21H2,the key ProductName of the the value is Windows 10 Pro.It is not correct.
Does anyone have another solution?

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
Jerry Dora
  • 53
  • 5
  • Does this answer your question? [Determine Operating System in .NET Core](https://stackoverflow.com/questions/38790802/determine-operating-system-in-net-core) – Dean Van Greunen Oct 18 '22 at 10:40
  • 2
    "For some reason,I need reduce my program size less than 20M." get rid of this totally arbitrary constraint? – nvoigt Oct 18 '22 at 10:40
  • 1
    @nviogt This is just a simple command line execution program. Maybe some people do not want an executable file to be more than 20M in size, which looks very bloated. – Jerry Dora Oct 18 '22 at 10:58
  • 1
    C and C# are very different languages. You should only tag language you are using. – Gerhardh Oct 18 '22 at 11:09
  • 1
    @Gerhardh I apologize. I modified the Tags by mistake while editing – Jerry Dora Oct 18 '22 at 11:11
  • 1
    @DeanVanGreunen This method cannot obtain os version like “Windows 2019 Enterprise LTSC”.It's hard to distinguish the specific version of windows. – Jerry Dora Oct 18 '22 at 11:12
  • @JerryDora retracted my close vote for duplication – Dean Van Greunen Oct 18 '22 at 11:49

0 Answers0