添加WiX自定义操作以将MSI日志文件复制到LOCALAPPDATA文件夹

时间:2014-03-26 01:52:10

标签: c# wix windows-installer custom-action

如何创作

的WiX自定义操作
  1. 总是在安装结束时调用,至少是否存在安装错误
  2. 将当前MSI日志文件从当前本地复制到用户的APPDATA文件夹
  3. 我有这个托管的自定义操作代码。不知道如何在我的Wix脚本中创建它的调用。是否应该在InstallFinalize之后安排自定义操作?可以安排OnExit ="错误"?

        [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();
                }
            }
        }
    

2 个答案:

答案 0 :(得分:2)

是的,您可以在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 - 属性。

答案 1 :(得分:1)

如果可以提供帮助,请不要在InstallFinalize之后搞乱自定义操作。在通过SCCM,Unicenter等工具进行部署时,通常会完全跳过它们。

一个自定义对话框,其中包含一个按钮,可以在安装结束时打开日志文件。但

相关问题