You've decided to try doing some Windows 7 programming? COOL! But what do you need to do to distinguish YOUR application from the 1.234 BILLION other Windows applications out there? You should probably be Googling/Binging the Windows API Code Pack, but BEFORE you write any lines of code with that API, you're going to need to figure out WHEN you're running on Windows 7 or Vista, or XP or 95. THAT's what this blog is about!
There are a BOAT load of blogs/articles/newsprint/technical trade rags talking about how to check for what version of Windows a user is running, but most of them are wrong, Wrong, WRONG! Why do I say that, they usually fail by missing a few key points. They usually miss the mark on mismatching major and minor versions by confusing a minor version of Windows 95 with say a major version of Windows 7. Sounds confusing, yup, cause it is, and hence why most algorithms get it wrong. Here's your frist clue it's wrong, they make it seem like it's rocket science and VERY COMPLICATED. It's not really. You just have to get a few things straight.
A few weeks back, SB and I saw Kate Gregory give a presentation about the Windows API Code Pack (watch this space for more details LOL) and she talked for about 15m about how to do it right and wrong. I took some of her ideas and applied them to my own little test project and here are two examples of what the outputs are!

The first screen cap is my program (code to follow) which is run under Windows XP (thanks to SB for his virtual machines while doing MSCD studying) and the second is my Windows 7 box.
As you can see, the code, correctly identifies which version of Windows being run by the application. What's the big deal you ask? Well, most importantly, if you know you're running under Windows 7, you can easily take advantage of the new Taskbar, jump lists, overlay icons, progress bars and even application restart and recovery apis! Those are just to name a few of the Windows 7 "things" you can use! Very cool stuff. But the FIRST thing you need to do, is identify when you are running on Windows 7.
So how do you do it? Well, at first you could look at the Environment.OSVersion.Platform property, but I found it lacking in granularity, especially with regards to reporting Windows Vista vs Windows 7. It has quite the detail with early versions of Windows, but not so much with the later stuff. Using the Environment.Version property seems to be the best bet here. Next, you have to be VERY careful about checking major AND minor versions. If you're not careful you could code logic to accept one major version but not a minor one. You have to watch your use of "<=" and ">=" logic signs.
I'm not saying mine's perfect, but I modelled it off of Kate Gregory's and I think it's pretty darn good.
string versionString;
Image versionImage;
if( Environment.OSVersion.Version.Major == 5 )
{
//doesn't really care about minor versions at this point
versionString = "XP";
versionImage = WindowsVersionChecker.Properties.Resources.WindowsXP;
}
else if( Environment.OSVersion.Version.Major == 6 )
{
//key here is the >= to ensure you catch 6.2 and 6.3 and +++
if( Environment.OSVersion.Version.Minor >= 1 )
{
versionString = "Windows 7";
versionImage = WindowsVersionChecker.Properties.Resources.Windows7;
}
else
{
versionString = "Vista";
versionImage = WindowsVersionChecker.Properties.Resources.WindowsVista;
}
}
else if( Environment.OSVersion.Version.Major >= 7 )
{
//doesn't really care about minor versions at this point,
//BUT we DO want our app to still be able to run right?
versionString = "Something even better than Windows 7? COOL!";
versionImage = WindowsVersionChecker.Properties.Resources.Windows8;
}
else
{
//maybe we still want our apps to run on older computers?
versionString = "Are you using Windows 95 or ME?";
versionImage = WindowsVersionChecker.Properties.Resources.Windows95;
}
versionLabel.Text = versionString + " (" + Environment.OSVersion.Platform + ")";
logoPictureBox.Image = versionImage;
The main idea here is, IF you're going to check the Major version, use equality mainly, THEN go with the assumption anything AFTER the version of Windows you're using currently, will and can support the stuff you are expecting to be there with that version. For versions previous, please, Please, PLEASE make sure you don't screw up the logic so your app GPFs or just closes on a user without any warning/clue as to why? Make sure you have an else statement to catch these cases.
So, there you have it. A pretty sure-fire way of checking for which version of Windows you're app is running under. Why do this? Cause with Windows 7, you'll be able to take advantage of some pretty cool things (which I hope to blog about very soon!).
Now it's time to grab a coffee and get coding!
Resources:
Source Code: http://www.pchenry.com:8080/svn/blog/trunk/2010/WindowsVersionChecker
Kate Gregory's Blog
MSDN: Operating System.Platform
MSDN: Environment.Version