Thursday, September 9, 2010

Is patch'ed? | C#

Patches are usually being applied to all the products/softwares to come around various issues that the end users do face over the period of time while using any software. These are also called with other names as Hot or Quick fixes, which are normally being identified by a number prefixed with KB (e.g KB2508987).

While creating setup (or InstallShield) applications, It is usually a case of detecting whether or not a specific patch is installed?. Knowing that, the installer can prompt the user to add it first before moving on with the installation. The below is a C# code that will detect whether a particular patch has been applied for particular product.

Follow the below 3 easy steps:
1) Add msi.dll to the project.

2) Add WindowsInstaller namespace.
using WindowsInstaller;
3) Add the below code to detect the patch.
Type type = Type.GetTypeFromProgID("WindowsInstaller.Installer", true);
if (type != null)
{
    WindowsInstaller.Installer installer = (WindowsInstaller.Installer)   Activator.CreateInstance(type);
    if (installer != null)
    {
        WindowsInstaller.StringList patches = installer.get_Patches("{049DB35D-4A8B-4DFF-A9EF-D7142186BDFD}");

        foreach (string patch in patches)
        {
           if (patch == "{011FF98E-2A9E-1AEE-D2DE-F2135786CDEF}")
           {
               // found
               break;
           }
        }
    }
}

No comments :

Post a Comment