[英]How do I cancel and rollback a custom action in VS2010 Windows Installer?
I have a custom action that adds/removes a certificate from the Trusted Root Certificates via the Windows Installer. 我有一个自定义操作,通过Windows Installer从受信任的根证书添加/删除证书。 I achieve this by using a CustomAction
我通过使用CustomAction实现了这一点
It's possible that the user may not have permissions to add the certificate to TrustedRoots, or they may select "Cancel", how do I roll back the previous actions, and tell the installer that I've cancelled the process? 用户可能没有权限将证书添加到TrustedRoots,或者他们可能选择“取消”,如何回滚以前的操作,并告诉安装程序我已取消该过程?
As it stands now the Windows Installer is always reporting a success response even if it fails. 现在,Windows Installer即使失败也始终报告成功响应。
You should set up your custom action to the a function with return type of ActionResult that way you can return the failure type if the cancel happens or another exception. 您应该将自定义操作设置为返回类型为ActionResult的函数,以便在发生取消或其他异常时返回失败类型。
using Microsoft.Deployment.WindowsInstaller;
namespace CustomAction1
{
public class CustomActions
{
[CustomAction]
public static ActionResult ActionName(Session session)
{
try
{
session.Log("Custom Action beginning");
// Do Stuff...
if (cancel)
{
session.Log("Custom Action cancelled");
return ActionResult.Failure;
}
session.Log("Custom Action completed successfully");
return ActionResult.Success;
}
catch (SecurityException ex)
{
session.Log("Custom Action failed with following exception: " + ex.Message);
return ActionResult.Failure;
}
}
}
}
NOTE: This is a WIX compatible custom action. 注意:这是一个兼容WIX的自定义操作。 I find WIX to allow for more control over the MSI creation.
我发现WIX允许更多地控制MSI创建。
Try to throw an InstallException. 尝试抛出InstallException。 In this case installer will detect thomething wrong with the installation and rollback actions.
在这种情况下,安装程序将检测安装和回滚操作的错误。
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
Console.WriteLine("Commit ...");
// Throw an error if a particular file doesn't exist.
if(!File.Exists("FileDoesNotExist.txt"))
throw new InstallException();
// Perform the final installation if the file exists.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.