How to use relative path from custom wix action?

I am creating a custom installer. Where I added Utility.CA.dll to perform my custom action. In this case, I want to access the local file relative to the path to setup.msi . The custom action method may use the direct path e:\utility\myfile.txt , but I cannot find the path ".. \ utility \ myfile.txt". After some experiment, I got that Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) shows the folder C:\Users\current_username\AppData\Local\Temp\MSIF384.tmp- .

What can I do? need wix code example.

+2
installer c # wix
source share
3 answers

First, I have to mention that it is very difficult to refer to a file relative to the .MSI source, because later (restore, repair, delete) the .MSI file will be executed from the installer cache and free files (for example myfile.txt ). You need to write your own action very carefully to deal with this fact.

What you are looking for is a Directory with the identifier SourceDir . You can get the SourceDir value by calling:

  string sourceDir = session["SourceDir"]; 

Note. I assume that you are using DTF where the session object is provided to your managed custom action.

Now the difficulty is that the SourceDir property is set only when the Windows installer ResolveSource . During the initial installation, when the .MSI file is double-clicked, the source will be resolved (since it is the initial installation and will require files). Subsequent installations may not require a source (for example, you will not need to return the CD back to disk for uninstallation). Therefore, you will need to invoke the ResolveSource action in your .MSI installation sequence (which will prompt the user to provide the .MSI source file again) or write a custom action code so that it does not require SourceDir in all cases.

You can read a little more about SourceDir here: http://robmensching.com/blog/posts/2010/1/26/stackoverflow-what-does-namesourcedir-refer-to

+2
source share

If you need to get the path to the folder where the msi file is located, you can use this fragment to get it:

 Path.GetDirectoryName(session["OriginalDatabase"]) 

The OriginalDatabase property can be used in InstallUISequence and InstallExecuteSequence settings.

To access the file regarding your msi you should use

 Path.Combine(Path.GetDirectoryName(session["OriginalDatabase"]), "myfile.txt") 
+1
source share

This works for me; in Product.wxs:

 <Binary Id="WixMyCustomActions" SourceFile="..\WixMyCustomActions\bin\WixMyCustomActions.CA.dll" /> <CustomAction Id="MyMethod" BinaryKey="WixMyCustomActions" DllEntry="MyMethod" Execute="immediate" Return="check" /> 

WixMyCustomActions.CA.dll is a C # class library in the same solution as a Wix project. In the project properties of WixMyCustomActions.CA.dll, Build Events, I have a post build event to copy WixMyCustomActions.CA.dll and WixMyCustomActions.CA.pdb from bin \ Debug or bin \ Release to bin:

 copy "$(TargetDir)*.dll" "$(ProjectDir)bin" /Y copy "$(TargetDir)*.pdb" "$(ProjectDir)bin" /Y 

By copying the dll, my Product.wxs will refer to any configuration (Debug or Release) that was built last.

Edit: to get the file regarding your CA dll, use this to find the CA build directory:

 using System.IO; using System.Reflection; // etc string assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 

Now you can find files relative to this directory.

0
source share

All Articles