views:

440

answers:

6

Hi!

I am looking for an elegant way to get the OS version like: "Windows XP Professional Service Pack 1" or "Windows Server 2008 Standard Edition" etc. Is there an elegant way of doing that? I am also interested in the processor architecture (like x86 or x64).

cheers

+2  A: 

For example like that:

EDIT: Yes, unfortunately it seems that with each new version of Windows we get a different mechanism ;)

Anonymous
first link looks good. however, I really wish there's a better - future proof way. You always need to maintain the OS versions in the code...thanks again
Stefan Koell
+2  A: 

One thing to be careful of is this information is usually localized and will report differently depending on the language of the OS.

You can get a lot of info from WMI look for the Win32_OperatingSystem class

JoshBerke
WMI seems to be future proof as it returns the friendly name out of the box without any conversion...Will take a closer look. Thanks...
Stefan Koell
+1  A: 

Note that the processor architecture question is complex:

do you mean (higher numers require lower numbers to be true):

  1. The CPU is capable for handling 64bit (in the sense that it supports AMD/intel x64 or Itanium)
  2. The Operating system is 64bit
    • GPR and pointers are 64bits, i.e. XP 64, Vista 64, a 64 bit server release or a 64bit OS for mono
  3. The currently executing process is a 64 bit process (not executing under Wow64 for example)

if you are happy that all 3 must be true then

IntPtr.Size == 8

Indicates that all three are true

ShuggyCoUk
Isn't this the same as `IntPtr.Size == 8`?
Hosam Aly
I understand that this question is complex - as you said. But I am just interested which Framework Version is handling my executable. So I thing the IntPtr method will suffice.
Stefan Koell
@Hosam: Not exactly. IntPtr.Size is the right thing to do though.
configurator
@configurator: Could you kindly explain how they are different?
Hosam Aly
+2  A: 

Why not use Environment.OSVersion? It will also tell you what operating this is - Windows, Mac OS X, Unix, etc. To find out if you are running in 64bit or 32bit, use IntPtr.Size - this will return 4 bytes for 32bit and 8 bytes for 64bit.

configurator
+1  A: 

You can use WMI to get the product name ("Microsoft® Windows Server® 2008 Enterprise "):

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

Check this: http://www.csharp411.com/determine-windows-version-and-edition-with-c/

Its a nice and comprehensive C# library that gets a good amount of OS info.

giddy