I hope the following will save you some time when dealing with custom windows installers.
If you want to access the installation path in your customer installer class, your first instinct might be to use a [TARGETDIR] or [INSTALLDIR]
as a custom action (for example /DIR=[TARGETDIR]) and attempt to access that in your custom installer class (for example. dir = Context.Parameters[“DIR”]). This will fail, since the TARGET is populated AFTER the custom action is executed.
I printed out the contents of the Context's parameter collection, that is available to the Custom Action, as shown below:
Writing the contents of the Installation Context
------------------------------------------------------------
Key: [dir] Value:
Key: [action] Value: install
Key: [installtype] Value: notransaction
Key: [assemblypath] Value: C:\Program Files\Company\Company Product\CompanyProduct.exe
Key: [logfile] Value:
------------------------------------------------------------
As seen above, you do have access to the [AssemblyPath] in the Custom Action and by stripping of the product.exe filename,
you can get the directory.
Here is a simple function that will achieve this:
private
string StripDir(string fullPath)
{
string retValue = default(string);
if (fullPath != null && fullPath != string.Empty && fullPath != default(string))
{
retValue = fullPath.Substring(0, fullPath.LastIndexOf(@"\"));
}
return retValue;
}
So the final code will look like this:
string installDir = StripDir(Context.Parameters[“AssemblyPath“]);