How does WinForms best determine which Windows operating system it is running on?

I have a WinForms application that should behave in certain ways (in particular, a shell for a specific installer) based on the operating system on which it is running.

I use the System.OperatingSystem class and combine the PlatFormID, Major, Minor, and Build numbers that you like best.

Unfortunately, the properties of the OperatinSystem object do not accurately distinguish between some platforms. For example. Vista and Windows Server 2008, or Vista 32 bit and Vista 64 bit. Similarly, the XP 64-bit Professional has the same version information as Server 2003.

So, is it possible to determine exactly which Windows operating system you are running from from a WinForms application (using C #)?

+5
source share
5 answers

The easiest way to distinguish between 32 bits and 64 bits is to use an environment variable PROCESSOR_ARCHITECTURE.

string value = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");

if you run this code on 32-bit Windows, it valuewill be either "x86" or empty. On 64-bit Windows, I assume that it will be installed on everything except "x86". It’s kind of messy, but so far it works in all versions of Windows where you can run the .NET program.

WMI , , Windows 2000 . , .

+4

WMI Win32_OperatingSystem.

, WMI Code Creator:

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher = 
                    new ManagementObjectSearcher("root\\CIMV2", 
                    "SELECT * FROM Win32_OperatingSystem"); 

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Win32_OperatingSystem instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("BuildNumber: {0}", queryObj["BuildNumber"]);
                    Console.WriteLine("Caption: {0}", queryObj["Caption"]);
                    Console.WriteLine("OSArchitecture: {0}", queryObj["OSArchitecture"]);
                    Console.WriteLine("OSLanguage: {0}", queryObj["OSLanguage"]);
                    Console.WriteLine("Version: {0}", queryObj["Version"]);
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }
}
+3

, ... , , , , .

If Environment.OSVersion.Platform = PlatformID.Win32NT Then
        If major <= 4 Then
            ret = "Windows NT 4.0"
            _usingNT4 = True
        ElseIf major > 6 Then
            ret = "Windows Vista"
        ElseIf major = 5 And minor = 0 Then
            ret = "Windows 2000"
        Else
            ret = "Windows XP"
        End If
    Else
        If major > 4 Or (major = 4 And minor >= 90) Then
            ret = "Windows ME"
        ElseIf (major = 4 And minor >= 10 And minor < 90) Then
            ret = "Windows 98"
        Else
            ret = "Windows 95"
        End If
    End If
+2

If you really need all the details, I think you can still use the good GetVersionEx Win32 API.

This is actually not .NET (strictly speaking), but applicable in a .NET application. See here .

+1
source

Here's a simpler way:

string os = Environment.OSVersion.VersionString;

... For my OS, the above returns the following:

Microsoft Windows NT 6.1.7600.0

Hope this helps.

+1
source

All Articles