How do I detect the currently installed features during a MajorUpgrade using WiX Burn MBA Bundles?

﹥>﹥吖頭↗ 提交于 2019-11-30 18:48:24

DetectMsiFeature is telling you the state of features for the new package; it's not installed, so obviously the features aren't. DetectRelatedMsiPackage gives you the data you need to query the feature states of the installed version using the (native) MSI API functions MsiEnumFeatures and MsiGetFeatureState/MsiGetFeatureUsage.

John M. Wright

I'm marking Bob Arnson's response as the answer, since it gave me what I needed to push this along, but for others who come across this post, I thought I'd give a little more detail about how you can gather the feature states using the WiX-provided ProductInstallation class (found in the Microsoft.Deployment.WindowsInstaller.dll assembly, which is in the WiX SDK), thus removing the need to make your own direct calls to native MSI API.

Here's an example of a method that could be registered to the DetectRelatedMsiPackage event. Note that you'll need to store off the information you gather so that you can set the appropriate states during the Plan phase.

private void DetectRelatedMsiPackageHandler(object sender, DetectRelatedMsiPackageEventArgs e)
{
    var existingPackageProductCode = e.ProductCode;
    var actionToBeAppliedToExistingPackage = e.Operation;
    var existingPackageId = e.PackageId;
    var existingPackageVersion = e.Version;

    Log(string.Format("Detected existing related package {0} (product: {1}) at version {2}, which will be {3}",
                      existingPackageId, existingPackageProductCode, existingPackageVersion,
                      actionToBeAppliedToExistingPackage));

    if (actionToBeAppliedToExistingPackage == RelatedOperation.MajorUpgrade)
    {

        //requires reference to WiX Toolset\SDK\Microsoft.Deployment.WindowsInstaller.dll
        var installedPackage = new Microsoft.Deployment.WindowsInstaller.ProductInstallation(existingPackageProductCode);
        if (!installedPackage.IsInstalled) {
            Log(string.Format("Migrating Package {0}, which is not installed, so marking it and it's features as Absent", existingPackageId));
            //TODO: add logic to store state so that during Plan phase can set package with package with product code = existingPackageProductCode to PackageState.Absent
        } else {
            Log(string.Format("Migrating features for MajorUpgrade of Package {0}", existingPackageId));

            foreach (var currentInstallFeature in installedPackage.Features) {                        
                if (currentInstallFeature.State == InstallState.Local) {
                    Log(string.Format("Migrating feature {1} of Package {0} - marking as Present", existingPackageId, currentInstallFeature.FeatureName));
                    //TODO: add logic to store state so that during Plan phase can set package and feature states based on this info
                } else {
                    Log(string.Format("Migrating feature {1} of Package {0} - marking as Absent", existingPackageId, currentInstallFeature.FeatureName));
                    //TODO: add logic to store state so that during Plan phase can set package and feature states based on this info
                }
            }
        }
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!