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).
Asked
Active
Viewed 1.7k times
26

AZ_
- 21,688
- 25
- 143
- 191

AngryHacker
- 59,598
- 102
- 325
- 594
3 Answers
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
-
Is PlatformID still Win32NT if the OS is 64 bit? – AngryHacker Apr 28 '10 at 20:06
-
2@AngryHacker PlatformID.Win32 = "The operating system is Windows NT or later.", so yes. – ParmesanCodice Apr 28 '10 at 20:54
-
1I changed the code for IsWinXp to check whether it's Windows XP or higher. Same with Vista. – AngryHacker Apr 28 '10 at 21:42
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).

Richard
- 106,783
- 21
- 203
- 265
-
3Note 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
-
-
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