views:

150

answers:

3
+1  Q: 

.NET for Windows 7

I want to take advantage of the new Windows 7 taskbar functionality in a .NET application and would like to know what people think is the best way to check for Windows 7 features in .NET. My aim is to have distinct code for Windows XP and Windows 7.

I'm not overally keen on:

  • just wrapping the Windows 7 code in a try catch
  • if OS = "Windows 7" Then...

Unless of course this is what most are doing. I really have hunted everywhere for a good approach but without success. The Microsoft unmanaged code wrapper library seems to just assume it's running on Windows 7...

+2  A: 

I would expect you do:

if ( Environment.OSVersion.Platform == windows7Id)
{
    // win7 stuff
}

Don't know what value the Windows7 id is though.

I typically ask this question to decide which object I create from my factories to support different platforms. Nicer than sprinkling these all over the code base or catching exceptions.

Quibblesome
+4  A: 

I think there's nothing wrong with if OS = "Windows 7". Of course,

  • it should be if OS >= "Windows 7" (so that it doesn't break in Windows 7.1 or whatever will be the successor) and
  • it should be done by checking Environment.OSVersion (Platform and >= Version).

If you don't want to clutter your code with if (isWindows7) {...}, you might want to put the platform-specific code into a separate class. For example, you could create an interface

interface IOSSpecific {
    void AddToJumpList(...);
    ...
}

and two implementations of this class, one for >= Win 7 and one for others (which does different things or nothing at all). When your application starts, you instantiate some global variable of type IOSSpecific with either one of the two implementations.

Heinzi
+1  A: 

If you use a wrapper library like Code Pack (http://code.msdn.microsoft.com/WindowsAPICodePack ) it does the version checking for you. You go ahead and call the library methods and it will throw a PlatformNotSupportedException if you try something that's not on the OS when you're runnning.

You can also catch EntryPointNotFoundException if you're P/Invoking into some Windows DLL. This is the best approach when doing your own interop, because functionality may get added (by updates and service packs) years from now into downlevel OS.

With both of these, you can cache some sort of flag that reminds you there is no taskbar or there are no libraries or you don't have whatever features, so as to save the perf hit of throwing and catching the exception.

If you insist on doing your own version checking and seeing what OS you are on, please remember the magic of >=. You know how much code is out there that tests the version is exactly XP SP2 and then puts up a message box saying "Requires XP SP2 or later"? A ton. In fact, that's why the major version for Win7 is 6 -- so that all the code checking for "equal to 6" would still work. Don't be that guy.

if (Environment.OSVersion.Version.Major >= 6)
{
   if (Environment.OSVersion.Version.Minor >= 1)
     // Do Win7 thing
   else
     // Do Vista thing
}
else if (Environment.OSVersion.Version.Major >= 5)
 // Do XP thing

You can find all the major/minor numbers at http://msdn.microsoft.com/en-us/library/ms724832%28VS.85%29.aspx

Kate

Kate Gregory
Actually, this code will do the Vista thing for 7.0, 8.0 and other mythical future versions. Rats, version checking is hard! Only check the minor version when you know exactly the major version.
Kate Gregory