首页 > 解决方案 > 使用 Wix 重命名现有文件夹

问题描述

我们的应用程序安装在我们通常无法访问的离线网络中。它也是一个高可用性应用程序。我们需要我们的旧应用程序文件夹仍然可供用户使用,因此我们希望在安装新应用程序文件夹之前重命名当前应用程序文件夹。这样,如果我们有一些不可行的错误,我们需要它们能够快速恢复到旧程序。

Wix可以做到这一点吗?

此外,我们知道这并不理想,但这是我们所拥有的,所以请回答问题而不是说“不要那样做”。

标签: installationdeploymentwixwindows-installercontinuous-deployment

解决方案


只需创建将在 CostFinalize 之前开始的自定义操作并移动您的文件夹。例如:

<InstallExecuteSequence>
     <Custom Action="RenameFolder"
             Before="CostFinalize"/>
</InstallExecuteSequence>

<CustomAction Id="RenameFolderCustomAction" BinaryKey="YourCustomActionDll" DllEntry="RenameFolderMethod" Execute="immediate" Impersonate="no" Return="check" />

您的自定义操作将如下所示:

[CustomAction]
public static ActionResult RenameFolderMethod(Session session)
{
    session.Log("Begin RenameFolderMethod");
    Directory.Move(source, destination);
    return ActionResult.Success;
}

此外,您需要添加自定义操作,以便在出现错误或取消时将其复制回来。为此,您可以使用 OnExit 自定义操作。

<InstallExecuteSequence>
     <Custom Action="RenameFolder" Before="CostFinalize"/>
    
     <Custom Action="InstallationFailed" OnExit="cancel" />
     <Custom Action="InstallationFailed" OnExit="error" />
</InstallExecuteSequence>

<CustomAction Id="InstallationFailed" BinaryKey="YourCustomActionDll" DllEntry="InstallationFailedMethod" Execute="immediate" Impersonate="no" Return="check" />

动作也是一样的,只是参数颠倒了:

[CustomAction]
public static ActionResult InstallationFailedMethod(Session session)
{
    session.Log("Begin InstallationFailedMethod");
    Directory.Move(destination, source);//move it back
    return ActionResult.Success;
}

您还可以使用属性来存储源路径和目标路径。如果需要,您甚至可以在运行 msi时定义它们。

一般如何添加自定义操作


推荐阅读