26

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).

AZ_
  • 21,688
  • 25
  • 143
  • 191
AngryHacker
  • 59,598
  • 102
  • 325
  • 594

3 Answers3

45

Use the System.OperatingSystem object, then 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);
}
Tinsa
  • 1,270
  • 12
  • 20
ParmesanCodice
  • 5,017
  • 1
  • 23
  • 20
9

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).

List of Windows Version Numbers on MSDN.

Richard
  • 106,783
  • 21
  • 203
  • 265
  • 3
    Note that WinXP 32-bit is version 5.1; WinXP 64-bit is version 5.2. – William Leara Apr 28 '10 at 19:18
  • 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 Apr 28 '10 at 19:57
5

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
  • 3,978
  • 17
  • 20
  • Given that an dependency is the reason he needs the version, that is a really good idea. – daramarak Apr 28 '10 at 19:49
  • How would you check whether the OS support RegFree COM? – AngryHacker Apr 28 '10 at 20:05
  • Try to load a component from your manifest. If it doesn't work, the platform doesn't support regfree COM – Stewart Apr 28 '10 at 21:19
  • I'm somewhat torn on this particular answer. If there is a specific API, for example, that can be checked for (simply), then I do agree that checking for that API is indeed the better route. However, I am in the situation that I needed to know if a specific system utility exists, which changed from XP to Vista. The cleanest way I can think to check this is just check OS version, not try and start some non-existent process and check for failure! If the utility I'm looking for doesn't exist after the version check, the OS is corrupted anyway. – monkey0506 Dec 27 '14 at 07:34
  • Then check for the existence of the utility. Checking the os version is so easy to get wrong, and if the user shims your process with a version lie checking the version doesn't tell you what you want to know anyway. – Stewart May 14 '15 at 05:54