views:

159

answers:

3

I am trying to play with the Environment.OSVersion.Version object and can't really tell what version would indicate that the OS is Windows XP or higher (e.g. I want to exclude Windows 2000, ME or previous versions).

+6  A: 

Check the Major property is greater than or equal to 5, and if 5 then Minor is at least 1. (XP was 5.1, 2003 was 5.2, Vista/2008 were 6.0).

Richard
...and Windows 7 is 6.1
Esko
Note that WinXP 32-bit is version 5.1; WinXP 64-bit is version 5.2.
William Leara
And this is exactly why you should look for the thing you need instead of checking the version number, and why Win7 is version 6.1 and not 7.0. Checking the version number is easy to get wrong, and doesn't always tell you what you wanted to know.
Stewart
+8  A: 

Use the System.OperatingSystem object, then just filter on the Major & Minor version numbers.

I've used these functions in the past:

static bool IsWinXPOrHigher
{
    OperatingSystem OS = Environment.OSVersion;
    return (OS.Platform == PlatformID.Win32NT) && ((OS.Version.Major > 5) || ((OS.Version.Major == 5) && (OS.Version.Minor >= 1)));
}

static bool IsWinVistaOrHigher
{
    OperatingSystem OS = Environment.OSVersion;
    return (OS.Platform == PlatformID.Win32NT) && (OS.Version.Major >= 6);
}
ParmesanCodice
Is PlatformID still Win32NT if the OS is 64 bit?
AngryHacker
@AngryHacker PlatformID.Win32 = "The operating system is Windows NT or later.", so yes.
ParmesanCodice
@ParmesanCodice, I changed the code for IsWinXp to check whether it's Windows XP or higher. Same with Vista.
AngryHacker
+3  A: 

You shouldn't check the version number. Instead, you should check for the functionality you need. If it is a specific API you're after for example, LoadLibrary and GetProcAddress it - that way, you're not dependent on the version number.

Stewart
Given that an dependency is the reason he needs the version, that is a really good idea.
daramarak
How would you check whether the OS support RegFree COM?
AngryHacker
Try to load a component from your manifest. If it doesn't work, the platform doesn't support regfree COM
Stewart