安装WiX后打开PDF文件,不显示任何错误

时间:2015-02-04 21:22:19

标签: wix shellexecute wix3.8

我想在WiX安装程序完成后打开PDF文件。

我目前拥有的相关WiX XML是:

<Property Id="WixShellExecTarget" Value="[#Manual.pdf]" />

<CustomAction Id="ShowManual" 
    Return="ignore" 
    BinaryKey="WixCA"
    DllEntry="WixShellExec" 
    Impersonate="yes" />

<InstallExecuteSequence>
    <Custom Action="ShowManual" After="InstallFinalize">NOT Installed</Custom>
</InstallExecuteSequence>

这一切都适用于安装了PDF阅读器的机器。但如果没有,Windows会闪烁一条消息,说“Windows无法打开此类文件”。

如果存在与PDF文件关联的应用程序,是否有办法让WiX仅尝试调用ShellExecute?或者是否可以在没有显示任何错误的情况下使呼叫无声地失败?

1 个答案:

答案 0 :(得分:-1)

我通过创建一个&#39;立即&#39;在InstallFinalize之后运行的托管自定义操作,并在尝试打开应用程序之前使用FindExecutable检查应用程序是否与PDF文件关联:

[DllImport("shell32.dll", EntryPoint = "FindExecutable")]
private static extern long FindExecutable(string lpFile, string lpDirectory, StringBuilder lpResult);

[CustomAction]
public static ActionResult ShowPdf(Session session)
{
    var installDir = session["INSTALLDIR"];
    var pdfPath = Path.Combine(installDir, @"My Dir\My.pdf");

    var pdfReaderPath = new StringBuilder(1024);
    long lngResult = FindExecutable(pdfPath, String.Empty, pdfReaderPath);

    if ((lngResult >= 32) && (!String.IsNullOrWhiteSpace(pdfReaderPath.ToString())))
    {
        Process.Start(pdfPath);
    }

    return ActionResult.Success;
}