簡體   English   中英

WIX 如何從自定義操作訪問源文件

[英]WIX how to access source file from custom action

我有一個 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM