Using a WiX custom action to set a property's value

回眸只為那壹抹淺笑 提交于 2019-12-10 12:44:07

问题


I am modifying an existing WiX installer to handle updating an existing installation of one of our products. There are several values whose defaults are specified in properties. These properties are displayed to the user for editing and are then written to a custom configuration file by the existing installer.

My code needs to be smart enough to detect if it is doing a brand new install versus installing an older version. If it is doing a brand new install, it needs to set the properties to default values. But if it is doing an upgrade, the code needs to retrieve the valus of those properties from the existing configuration file and display those to the user.

From the reading I've done, it seems to me I need to use a type 51 custom action to set the properties. But how do I implement this custom action?

I'm thinking that I have to first define the custom action to put it in the custom action table, and then I need to stick a tag somewhere to call it. And then I need to define it.

How can I do this? What would some example code be?


回答1:


After doing some more research into custom actions, I believe I've got all of this figured out. I added a <Binary> tag to the .wxs file to identify where the custom action resides. I then referenced the Binary tag's ID in the CustomAction. Finally, I added a Custom tag to the InstallExecuteSequence section that referenced the CustomAction tag by ID.

The final Custom tag mentioned above needs to go into the InstallUISequence section, not the InstallExecuteSequence section, as the custom action needs to be called before the dialog is displayed.

As for the implementation of the Custom Action itself, I added a new C# Custom Action library project to the solution. In there, I implemented a method, decorated with the [CustomAction] attribute. This method uses the values of properties stored in the Session object passed as a parameter to the method and determines the path of the current version's executable file. It then does the work needed to locate the values in the program's Configuration file that need to be preserved across versions and writes them to other properties for the upgrade script.




回答2:


Read the following sections of WiX tutorial:

  1. Extra Actions: gives an overview of how to add a Custom Action to MSI;
  2. What's Not in the Book: provides an example how to implement a Custom Action in DLL.



回答3:


Example:

    [CustomAction]
    public static ActionResult SetProperty(Session session)
    {
        try
        {
            session.Log("Begin SetProperty action");

            session["PROPERTY_NAME"] = "value"


        }
        catch (Exception exception)
        {
            session.Log("ERROR in custom action SetProperty {0}", exception.ToString());

            return ActionResult.Failure;
        }

        return ActionResult.Success;
    }


来源:https://stackoverflow.com/questions/7388795/using-a-wix-custom-action-to-set-a-propertys-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!