I have an MSI file being created with Wxs 3.0. My MSI references a C# custom action, written using the new C# Custom Action project.
I want to pass an argument to msiexec that gets routed to my custom action - for example:
msiexec /i MyApp.msi ENVIRONMENT=TEST#
In my .wxs file, I refer to my custom action like this:
<Property Id="ENVIRONMENT"/>
<Binary Id="WixCustomAction.dll" SourceFile="$(var.WixCustomAction.Path)" />
<CustomAction Id="WixCustomAction" BinaryKey="WixCustomAction.dll" DllEntry="ConfigureSettings"/>
<InstallExecuteSequence>
<Custom Action="WixCustomAction" After="InstallFiles"></Custom>
</InstallExecuteSequence>
My C# custom action is set up like this:
[CustomAction]
public static ActionResult ConfigureSettings(Session session)
{
}
I was expecting to be able to access the property like this:
string environmentName = session.Property["ENVIRONMENT"];
but this doesn't seem to work.
How do I access the property I passed to msiexec in my custom action?
Here is my working code:
In the C# custom action function:
Just for completeness; using the method described by Jeremy Lew, in the blog above allows for the following:
Calling:
With this in the .wxs file:
With a custom action:
works.
Parsing the CustomActionData arguments is pretty ugly, but it does work. Hopefully someone knows a more elegant way to do this.
Your custom action needs to be a deferred custom action in order to run after InstallFiles. Deferred custom actions do not have access to properties, but they do have access to CustomActionData. See this blog post for a discussion on how to get what to do about it. (This example is a VBScript custom action, but you will be able to retrieve the value through the session.CustomActionData collection.)
If we're talking about Wix Sharp (and not plain Wix with its XML stuff), adding a custom property is a piece of cake. All you have to do is to set UsesProperties property for your managed action.
For example, if you want to add a custom property named "MYPROP", just define your action like this:
Set the property value via msiexec command line:
And then you'll be able to access it from your custom action:
When property is not set via command line the default value will be an empty string.
If instead of
you write this:
then you will be able to reference your variables like this: