How to programmatically conclude whether the OS is Windows XP
Hi,
I'm looking for a C# code snippet which concludes whether the OS is Windows XP, which is bullet proof and not risky (i.e., no strings manipulation on the name of the OS, since maybe there are Windows XP Editions which don't contain the exact word "Windows XP", and I wouldn't go for "string.Contains("XP")" either) ?
[If you have the same code snippet for Vista and Windows 7, it will be great]
Thanks at Advanced!
Re: How to programmatically conclude whether the OS is Windows XP
Envirnment.OSVersion
While I also like to avoid string manipulation when possible, I don't think worrying about Windows XP versions that don't contain the string "XP" is a valid concern as they all do and information on every version of windows out there is readily available. You could store each version string as a const field and then just use a switch to determine the version. That can return an enumerated value if you like.
You don't need to be overly defensive here.
Re: How to programmatically conclude whether the OS is Windows XP
Use the Environment.OSVersion.Version to get the major and minor versions.
Code:
OS Version number
Windows 7 6.1
Windows Server 2008 R2 6.1
Windows Server 2008 6.0
Windows Vista 6.0
Windows Server 2003 R2 5.2
Windows Server 2003 5.2
Windows XP 64-Bit Edition 5.2
Windows XP 5.1
Windows 2000 5.0
You may notice that this chart doesn't distinguish between the server and desktop versions, nor does it give you info on specific editions.
If you need this additional info, then you'll need to pinvoke to GetVersionEx.
See OSVERSIONINFOEX for more info.
Re: How to programmatically conclude whether the OS is Windows XP
Good call Arjay; for some reason I thought OSVersion was a string.