0

I am using .NET framework 3.5 version, and the program has to detect all the Windows versions(including Windows XP, Windows Vista, Windows 7, Windows 8, Windows 10, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012).

The problem is how can I determine the OS in below situations?

  • Windows Vista and Windows Server 2008 Version number both are 6.0.
  • Windows 7 and Windows Server 2008 R2 Version number both are 6.1.
  • Windows 8 and Windows Server 2012 Version number both are 6.2.

I have found the below code but I can't use because I'm using .NET Framework 3.5.

var name = (from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>()
                  select x.GetPropertyValue("Caption")).FirstOrDefault();
return name != null ? name.ToString() : "Unknown";

How can I solve this problem?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Andy
  • 407
  • 2
  • 14
  • 30
  • I do not know your specific need to determine the OS version. That said, as a general rule, you should test for whether functionality is present and not the OS version. Furthermore, almost all of the functionality between the client and server versions of Windows are the same. – Brian May 29 '15 at 03:59
  • http://stackoverflow.com/questions/577634/how-to-get-the-friendly-os-version-name – Tharif May 29 '15 at 05:14
  • The code above works fine in .NET 3.5. Make sure System.Linq is in your using clause and that you add a reference to System.Management.dll. – Kevin May 29 '15 at 06:47
  • Check https://learn.microsoft.com/es-es/dotnet/api/system.environment?view=net-6.0 – Carlos Garcia Jul 21 '22 at 08:20

3 Answers3

1

I'm assuming the problem with that code is that it uses LINQ. You can still use WMI to check it, just don't use LINQ. I also think it's better to check the ProductType rather than Caption.

using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"))
{
    foreach (ManagementObject managementObject in searcher.Get())
    {
        uint productType = (uint)managementObject.GetPropertyValue("ProductType");
        // productType will be 1 for workstation, 2 for domain controller,
        // 3 for normal server
    }
}

Then just check version number to determine actual OS version.

Another way is to use registry and check the key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ProductOptions\ProductType. This will have values WinNT, ServerNT or LanmanNT to mark the same options as the WMI code.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
  • Thank you! .NET framework 3.5 doesn't support LINQ, and your solution is perfect. Here is another problem, how can I fouind `ManagementObjectSearcher` and`ManagementObject` class? I have using `System.Management`. – Andy May 29 '15 at 05:11
  • They are in System.Management https://msdn.microsoft.com/en-us/library/system.management.managementobjectsearcher%28v=vs.90%29.aspx, make sure you have the assembly reference in place – Sami Kuhmonen May 29 '15 at 05:16
  • I have add System.Management to reference, it works! Thanks! – Andy May 29 '15 at 05:22
0

You can directly get information from registry file. It works perfectly from .net 3.0 and above.

String loc= @"SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion";
RegistryKey key = Registry.LocalMachine;
RegistryKey skey = key.OpenSubKey(loc);
Console.WriteLine("OS Name: {0}", skey.GetValue("ProductName"));
Dipitak
  • 97
  • 1
  • 1
  • 3
0

Good day

Maybe someone reach this question on 2022, so it is worth noting that you can use the "Environment" Class to get all kind of info about de computer you are running your application.

In my code, and making comparation with the different build numbers (https://www.gaijin.at/en/infos/windows-version-numbers) I am able to discriminate if running in Windows 7, 10 or 11.

Here's a snippet:

System.Environment ('or using System;')

if (System.Environment.Version.Build> 9200) { 'do something'}

Regards PD: Seems that has been supported for a long time... https://learn.microsoft.com/es-es/dotnet/api/system.environment?view=net-6.0

Carlos Garcia
  • 233
  • 1
  • 6