c# - WIX 如何从自定义操作访问源文件

标签 c# wix

我有一个 WIX 安装应用程序和许多源文件:

...
<Directory Id="dirF21F1AE09DCD1D651EFCA4E6AD334FAC" Name="myservice">
<Component Id="cmp503CB14E95C2C333DCE9E73FC1BB2E9A" Guid="{29FDDCA4-E70D-41AA-B1C8-06AD9A07810D}">
    <File Id="fil2FE62A0172300DF74F1725E28B7FA003" KeyPath="yes" Source="$(var.SourcePath)\myservice\common_base.dll" />
</Component>
</Directory>
...

并且此文件由 ref 复制:

<ComponentGroupRef Id="InstallSources"/>

在复制文件之前,我需要在自定义操作中访问 c​​ommon_base.dll。我想将它复制到临时文件夹进行一些操作:

private static void CopyCommonDll(Session session)
{
    try
    {
        var dllPath = session["get path here"]; // or can I get dllPath the other way?

        session.InfoLog("Dll path: {0}", dllPath);

        var destPath = Path.Combine(Path.GetTempPath(), Path.GetFileName(dllPath));

        session.InfoLog("destPath dll path: {0}", dllPath);

        File.Copy(dllPath, destPath);

        session.InfoLog("file copied!");

        // some code here

        File.Delete(destPath);
    }
    catch (Exception e)
    {
        session.ErrorLog(e);
    }
}

我该怎么做?

最佳答案

解决方案是将您希望在自定义操作安装期间访问的文件作为嵌入式资源添加到自定义操作的项目中。在自定义操作中,您可以从程序集资源中获取它。

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "CustomActions.Resources.common_base.dll";

byte[] bytes = null;
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
    if (stream != null)
    {
        session.InfoLog("dll found was in resources");

        bytes = new byte[(int)stream.Length];
        stream.Read(bytes, 0, (int)stream.Length);
    }
}

if (bytes != null)
{
    // save file here!
    File.WriteAllBytes(destPath, bytes);
}

关于c# - WIX 如何从自定义操作访问源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35743982/

相关文章:

com - 如何在插件体系结构中进行免注册的COM

c# - 实例化 Web 服务时的最佳做法是什么

c# - EasyHook 给定的 32 位库不存在,用户库没有导出正确的运行

c# - 将 CommandBindings 添加到控件与使用 RegisterClassCommandBinding 之间有区别吗?

.net - 我如何创建一个 Bootstrap ,它将安装第 3 方 exe,然后安装 2 个 msi 应用程序

installation - 如何在 WiX 安装程序中处理 ManagedBootstrapperApplicationHost 和重新启动/重新启动?

c# - 如果属性仅在它们被反射到时才被构造,为什么属性构造函数如此有限?

c# - ASP.NET Core API 版本控制具有 Major.minor 但仅在路径中包含 Major

wix - 为什么 Major Upgrade 不升级以前的 Per-Machine 安装?

installation - 如何将 MSI 文件添加到我的安装程序