我该如何编写一个WiX自定义操作


总是在安装结束时调用,至少在出现安装错误时
将当前MSI日志文件从其当前本地复制到用户的APPDATA文件夹中


我有此托管的自定义操作代码。不知道如何在我的Wix脚本中编写其调用。是否应该在InstallFinalize之后安排自定义操作?可以预定OnExit =“ error”吗?

    [CustomAction]
    public static void CopyLogFile(Session session)
    {
        const string company = "MyCompany";
        const string product = "MyProduct";
        try
        {
            session.Log("CustomAction.CopyLogFile entry");

            var msiLogFilePath = session.CustomActionData["LOGFILEPATH"];
            if (msiLogFilePath != null)
            {
                session.Log("CustomAction.CopyLogFile MSI log filename: {0}", msiLogFilePath);

                var localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                var destDirPath = Path.Combine(localAppDataPath, company, product);

                var destDir = Directory.CreateDirectory(destDirPath);
                session.Log("CustomAction.CopyLogFile Destination directory: {0}", destDir.FullName);

                var destFilePath = Path.Combine(destDir.FullName, Path.GetFileName(msiLogFilePath));
                File.Copy(msiLogFilePath, destFilePath, true);

                session.Log("CustomAction.CopyLogFile Log file copied to: {0}", destFilePath);
            }
            else
            {
                session.Log("CustomAction.CopyLogFile File path not found");
            }
        }
        catch (Exception exception)
        {
            session.Log("CustomAction.CopyLogFile exception {0}", exception);
        }
        finally
        {
            if (session != null)
            {
                session.Log("CustomAction.CopyLogFile exit");
                session.Close();
            }
        }
    }

最佳答案

是的,您可以在InstallFinalize之后计划它(我也是,但是如果不是完全删除软件包,我每次都会复制它):

<InstallExecuteSequence>
    <Custom Action="CopyLogfile" After="InstallFinalize">NOT (REMOVE="ALL" AND NOT UPGRADINGPRODUCTCODE)</Custom>
</InstallExecuteSequence>


如果有的话,请记住也将其添加到UI序列中。我将其作为事件添加到SetupCompleteSuccess-和SetupCompleteError-对话框中的PushButton中(也许您只需要将其添加到后一个中?),如下所示:

<Dialog Id="SetupCompleteSuccess" X="50" Y="50" Width="374" Height="266" Title="[ProductName]" NoMinimize="yes">
     <Control Id="OK" Type="PushButton" X="230" Y="243" Width="66" Height="17" Text="&amp;Finish" TabSkip="no" Default="yes" Cancel="yes">
           <Publish Event="EndDialog" Value="Exit">1</Publish>
           <!-- ### Invoking copying the logfile if the Finish-button is pressed -->
           <Publish Event="DoAction" Value="CopyLogfile">MsiLogFileLocation AND NOT (REMOVE="ALL" AND NOT UPGRADINGPRODUCTCODE)</Publish>
    </Control>
 <!-- ... rest of the dialog ... -->
</Dialog>


关于仅在出现错误的情况下显示它:也许检查ProductState属性?在网上搜索了此内容,但没有发现任何有用的信息。

编辑:也许仅在发生错误时执行它的正确方法是使用仅在回滚期间执行它的自定义操作标志。在WiX中,它是Execute="rollback"标签的CustomAction属性。

关于c# - 添加WiX自定义操作以将MSI日志文件复制到LOCALAPPDATA文件夹,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22650003/

10-13 07:58