Anton23
Anton23

Reputation: 2129

WIX how to access source file from custom action

I have a WIX install application and many source files:

...
<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>
...

And this files are copied by a ref:

<ComponentGroupRef Id="InstallSources"/>

I need to access common_base.dll in custom action before copying files. I want to copy it to temp folder for some manipulation:

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

How can I do this?

Upvotes: 5

Views: 2093

Answers (1)

Anton23
Anton23

Reputation: 2129

The solution is to add to Custom Action's Project as embedded resources a file you want to access during the installation in custom action. In custom action you can get it from the assembly resources.

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

Upvotes: 3

Related Questions