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

2024-03-08

我有一个 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>
...

这个文件是由引用复制的:

<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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WIX如何从自定义操作访问源文件 的相关文章

随机推荐