gpt4 book ai didi

c# - wix - 安装前删除旧的程序文件夹

转载 作者:太空宇宙 更新时间:2023-11-03 13:55:11 28 4
gpt4 key购买 nike

我需要安装程序删除旧的安装目录(如果存在),就在安装程序开始复制新文件之前。此文件夹包含程序使用期间生成的一些文件和子文件夹,它们不包含在安装程序中。因此,我创建了自定义操作来执行此操作。

所以,一些代码。首先,自定义操作代码(没什么特别的):

[CustomAction]
public static ActionResult RemoveOldDatabase(Session session)
{

bool removeDatabase = session.CustomActionData["RemoveDatabase"] == "true";
string installDir = session.CustomActionData["InstallDir"];

if (removeDatabase)
{
try
{
Directory.Delete(installDir, true);
}
catch (Exception ex)
{
session.Log(ex.StackTrace);
}
}

return ActionResult.Success;
}

和wix代码(它定义了自定义操作调用):

<CustomAction Id="actionCheckServerName" BinaryKey="actionBinary" DllEntry="CheckServerName" Execute="immediate" Return="check" />
<CustomAction Id="actionInstall" BinaryKey="actionBinary" DllEntry="Install" Execute="deferred" HideTarget="no" Impersonate ="no" Return="check"/>
<CustomAction Id="actionUninstall" BinaryKey="actionBinary" DllEntry="Uninstall" Execute="deferred" HideTarget="no" Impersonate ="no" Return="check"/>

<CustomAction Id="actionRemoveOldDatabase" BinaryKey="actionBinary" DllEntry="RemoveOldDatabase" Execute="deferred" HideTarget="no" Impersonate ="no" Return="ignore"/>


<CustomAction Id="actionGetNetworkComputers" BinaryKey="actionBinary" DllEntry="GetNetworkComputers" Execute="immediate" Return="check"/>

<CustomAction Id="SetInstallParameters" Return="check" Property="actionInstall" Value="InstallDir=[INSTALLDIR];ServerName=[SERVER_LIST];InstallMode=[SETUP_MODE];Single=[single];RemoveDatabase=[REMOVE_DATABASE]" />
<CustomAction Id="SetUninstallParameters" Return="check" Property="actionUninstsall" Value="UnInstallDir=[INSTALLDIR];ServerName=[SERVER_LIST];UnInstallMode=[INSTALL_MODE]" />

<CustomAction Id="SetRemoveOldDatabaseParameters" Return="check" Property="actionRemoveOldDatabase" Value="InstallDir=[INSTALLDIR];RemoveDatabase=[REMOVE_DATABASE]" />


<InstallExecuteSequence>
<Custom Action='AlreadyUpdated' After='FindRelatedProducts'>SELFFOUND</Custom>
<Custom Action='NoDowngrade' After='FindRelatedProducts'>NEWERFOUND</Custom>

<Custom Action="SetRemoveOldDatabaseParameters" Before="ProcessComponents"/>
<Custom Action="actionRemoveOldDatabase" After="SetRemoveOldDatabaseParameters">NOT Installed AND NOT UPGRADINGPRODUCTCODE</Custom>

<Custom Action="SetInstallParameters" Before="actionInstall"/>
<Custom Action="SetUninstallParameters" Before="RemoveFiles">Installed AND NOT REINSTALL AND NOT UPGRADINGPRODUCTCODE</Custom>
<Custom Action="actionInstall" Before="InstallFinalize">NOT Installed AND NOT UPGRADINGPRODUCTCODE</Custom>
<Custom Action="actionUninstall" After="SetUninstallParameters">Installed AND NOT REINSTALL AND NOT UPGRADINGPRODUCTCODE</Custom>
</InstallExecuteSequence>

有什么问题?如您所见,actionRemoveOldDatabase 应该在安装程序开始复制新文件之前触发(参数已由 SetRemoveOldDatabaseParameters 设置)。所以 - 只应删除旧文件 - 但这种情况不会发生。如果我这样做,操作actionRemoveOldDatabase,安装程序将新文件复制到安装目录后,安装目录将被删除。所以 - 安装程序复制的所有新文件都将被删除

我不明白为什么?如何只删除旧的、已经存在的文件夹以及为什么我的自定义操作会删除所有复制的文件?

[编辑]看来我已经知道原因了。在这种情况下,Install Dir 正在使用中(可能是 Windows 安装程序将其锁定),并且在安装结束后将其释放。自定义操作将等到文件夹被释放,然后将其删除。不幸的是,为时已晚 - 文件夹中已包含新文件。

您知道任何解决方法吗?

最佳答案

RemoveFile element正是为了做到这一点而设计的。您可以使用它来教 MSI 删除它未安装的应用程序数据。优点是在回滚期间,文件将放回原处。

您还可以使用 RemoveFolder 元素删除整个目录。一般的概念是 * 删除文件并指定文件夹。这不是递归的,因此您需要对可能已经创建的任何子目录执行此操作。

编写自定义操作只是重新发明轮子并增加安装程序的脆弱性。仅当无法事先知道子目录时才应使用它。在那种情况下,理想的情况是使用 MSI 中的临时行在安装时动态地将行发送到 MSI 中,并让 MSI 处理实际的删除。这允许回滚功能仍然有效。

这是一个非常简单的版本。它可以通过使数据从自定义表而不是 ComponentID 和 DirectoryID 的常量字符串驱动来改进。

 public class RecursiveDeleteCustomAction
{

[CustomAction]
public static ActionResult RecursiveDeleteCosting(Session session)
{
// SOMECOMPONENTID is the Id attribute of a component in your install that you want to use to trigger this happening
const string ComponentID = "SOMECOMPONENTID";
// SOMEDIRECTORYID would likely be INSTALLDIR or INSTALLLOCATION depending on your MSI
const string DirectoryID = "SOMEDIRECTORYID";

var result = ActionResult.Success;
int index = 1;

try
{
string installLocation = session[DirectoryID];
session.Log("Directory to clean is {0}", installLocation);

// Author rows for root directory
// * means all files
// null means the directory itself
var fields = new object[] { "CLEANROOTFILES", ComponentID, "*", DirectoryID, 3 };
InsertRecord(session, "RemoveFile", fields);
fields = new object[] { "CLEANROOTDIRECTORY", ComponentID, "", DirectoryID, 3 };
InsertRecord(session, "RemoveFile", fields);

if( Directory.Exists(installLocation))
{
foreach (string directory in Directory.GetDirectories(installLocation, "*", SearchOption.AllDirectories))
{
session.Log("Processing Subdirectory {0}", directory);
string key = string.Format("CLEANSUBFILES{0}", index);
string key2 = string.Format("CLEANSUBDIRECTORY{0}", index);
session[key] = directory;

fields = new object[] { key, ComponentID, "*", key, 3 };
InsertRecord(session, "RemoveFile", fields);

fields = new object[] { key2, ComponentID, "", key, 3 };
InsertRecord(session, "RemoveFile", fields);

index++;
}
}
}
catch (Exception ex)
{
session.Log(ex.Message);
result = ActionResult.Failure;
}

return result;
}
private static void InsertRecord(Session session, string tableName, Object[] objects)
{
Database db = session.Database;
string sqlInsertSring = db.Tables[tableName].SqlInsertString + " TEMPORARY";
session.Log("SqlInsertString is {0}", sqlInsertSring);
View view = db.OpenView(sqlInsertSring);
view.Execute(new Record(objects));
view.Close();
}
}

关于c# - wix - 安装前删除旧的程序文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12492769/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com