WIX如何从客户行动中访问源文件

时间:2016-03-02 10:05:18

标签: 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"/>

我需要在复制文件之前在自定义操作中访问common_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);
    }
}

我该怎么做?

1 个答案:

答案 0 :(得分:2)

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

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);
}