How to determine the installed version of BITS


The update published in our GotDotNet Workspace for Updater Application Block fixed the BITS based downloader included in the block. It doesn't include any intelligence to determine which the installed version of BITS is and act accodringly. A smarter downloader could determine at runtime which the installed version is in a particular desktop and then decide whether or not to use the new APIs available. Here are some code snippets that may help doing this:

 

using System;
using System.Runtime.InteropServices;

namespace Microsoft.ApplicationBlocks.ApplicationUpdater.Downloaders
{
       public class BITSVersionHelper
       {

            //2.0
           [ComImport, Guid("6d18ad12-bde3-4393-b311-099c346e6df9")] 
           internal sealed class BITS2_0
           { 
           }
           //1.5
           [ComImport, Guid("f087771f-d74f-4c1a-bb8a-e16aca9124ea")] 
           internal sealed class BITS1_5
           { 
           }
           //1.0
           [ComImport, Guid("4991d34b-80a1-4291-83b6-3328366b9097")] 
           internal sealed class BITS1_0
           { 
           }

           private static bool BITS10Installed;
           private static bool BITS15Installed;
           private static bool BITS20Installed;

           public static bool IsVersion10Installed
           {
               get { return BITS10Installed; }
           }

           public static bool IsVersion15Installed
           {
               get { return BITS15Installed; }
           }  
  
           public static bool IsVersion20Installed
           {
               get { return BITS20Installed; }
           }

           static BITSVersionHelper()
           {
               try
               {
                   BITS1_0 bits1_0 = new BITS1_0();
                   BITS10Installed = true;
               }
               catch
               { 
                   BITS10Installed = false;
               }
   
               try
               {
                   BITS1_5 bits1_5 = new BITS1_5();
                   BITS15Installed = true;
               }
               catch
               { 
                   BITS15Installed = false;
               }
               try
               {
                   BITS2_0 bits2_0 = new BITS2_0();
                   BITS20Installed = true;
               }
               catch
               { 
                   BITS20Installed = false;
               }
        }

        private BITSVersionHelper()
        {}
    }
}

As usual, all code samples are provided "AS IS" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

Comments (5)

  1. Mark says:

    Cool trick. I guess this could be used to check different versions of any COM object to exploit the newer features available provided their Guids are known beforehand.

  2. eugeniop says:

    Yes, it should work with any COM object.

Skip to main content