Question

System.Environment.OSVersion does not appear to indicate which Edition of Windows 2003 is installed (Standard, Enterprise, DataCenter).

Is there any way to access this information using managed code only?

I know I can use P/Invoke to call GetVersionEx and examine OSVERSIONINFOEX.wSuiteMask to get this info, but I'm looking for a simpler solution.

Update

Using WMI looks like the way to go, though the OSProductSuite property of Win32_OperatingSystem looks more reliable than the Name property. Here's sample code:

ManagementScope scope = new ManagementScope();
ObjectQuery query = new ObjectQuery("SELECT name, csdversion, description, OperatingSystemSKU, OSProductSuite FROM Win32_OperatingSystem");

using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
{
    using (ManagementObjectCollection resultCollection = searcher.Get())
    {
        foreach (ManagementObject result in resultCollection)
        {
            foreach (PropertyData propertyData in result.Properties)
            {
                Debug.WriteLine(
                    propertyData.Name + ": " +
                    ((propertyData.Value == null) ? "" : propertyData.Value.ToString())
                    );
            }
        }
    }
}
Was it helpful?

Solution

You could execute the following WMI query:

SELECT name FROM Win32_OperatingSystem

It returns something like this:

Microsoft Windows Server 2003 Standard Edition|C:\WINDOWS|\Device\Harddisk0\Partition1

This article explains how to perform WMI queries using .NET.

OTHER TIPS

I'm not aware of any way to do this using only managed code.

There is some code here using GetVersionEx that should encapsulate things for you nicely though.

I just wanted to add a smaller snippet of code for anyone who needs it.

    private static string GetOSName()
    {
        string result = string.Empty;
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem");
        foreach (ManagementObject os in searcher.Get())
        {
            result = os["Caption"].ToString();
            break;
        }
        return result;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top