What is the simplest way to differentiate between Windows versions?
How开发者_高级运维 do I know programmatically whether my OS is Longhorn server or Vista (client).
It seems the major version and minor version are same for both:
http://msdn.microsoft.com/en-us/library/ms724833.aspxSo, is there any better alternative?
Doesn't that same page you link give you the answer?
Windows Server 2008 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION
Windows Vista OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
Just check the wProductType
member against VER_NT_WORKSTATION
edit
Doesn't look like there's a way to get this without P/Invoke. System.Environment.OSVersion
doesn't expose this level of detail, and though there is an internal static class Win32Native
in the Microsoft.Win32
namespace in mscorlib
, as far as I can tell from disassembly, there's nothing that uses, let alone exposes, wProductType
.
I have found (but not tried) this page on pinvoke.net.
For Server,
OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION
For Workstation
OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
You can use WMI to find out windows version and all sorts of other system info as well.
Here is how to get a version string:
var osDetails = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<System.Management.ManagementObject>().First();
string version = osDetails.GetPropertyValue("Caption") as string;
this will return a string that reads something like: "Microsoft Windows XP Professional".
there are more properties there apart from Caption that can be used to retrieve version numbers, editions, architecture etc.:
http://msdn.microsoft.com/en-us/library/aa394239(v=VS.85).aspx
精彩评论