Reputation: 9464
I have a WinForms application that needs to behave in specific ways (specifically shell to a certain installer) based on the operating system on which it is running.
I am using the System.OperatingSystem class, and combining the PlatFormID, Major, Minor and Build numbers which gets you most of the way there.
Unfortunately, the properites of an OperatinSystem object, do not allow you to distinguish precisely between some platforms. E.g. Vista and Windows Server 2008, or Vista 32 bit and Vista 64 bit. Likewise, XP 64 bit Professional seems to have the same versioning info as Server 2003.
So is it possible to determine exactly which Windows operating system you are running on, from a WinForms App (using c#)?
Upvotes: 6
Views: 1611
Reputation:
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.
Upvotes: 1
Reputation: 292675
You can use WMI to retrieve information for the Win32_OperatingSystem
management class.
Code generated with 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);
}
}
}
}
Upvotes: 4
Reputation: 11972
If you really need all the details, I guess you still can use the good-old GetVersionEx of Win32 API.
In fact this is not .NET (strictly speaking), but usable in a .NET application. See here.
Upvotes: 1
Reputation: 25052
The easiest way to distinguish between 32bit and 64bit is through environmental variable PROCESSOR_ARCHITECTURE
.
string value = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
if you run this code on 32bit Windows, value
will be either "x86" or empty. On 64bit Windows I assume it will be set to anything but "x86". Kind of messy but so far it works on all versions of Windows where you can execute .NET program.
You can also use more modern WMI to query practically all information about operating system you can imagine but this will only work on Windows 2000 or newer. If you can live with that, check this blog post for some examples.
Upvotes: 4
Reputation: 1448
This was something i did about a year ago for a legacy app at my company... I don't know that it is the most current method, but it certainly worked.
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
Upvotes: 2