Blog Stats
  • Posts - 17
  • Articles - 2
  • Comments - 16
  • Trackbacks - 1

 

How to get the target directory (TARGETDIR) in a customer windows installer

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“]);


Feedback

# re: How to get the target directory (TARGETDIR) in a customer windows installer

Gravatar It is of great help 7/20/2007 7:14 AM | Manoj Banga

# re: How to get the target directory (TARGETDIR) in a customer windows installer

Gravatar better option then your StripDir(string) method:

String TargetDirectory = Path.GetDirectoryName(Context.Parameters["AssemblyPath"]); 5/21/2008 2:32 PM | Jason R. Shaver

# re: How to get the target directory (TARGETDIR) in a customer windows installer

Gravatar Even though Jason is correct, your idea for getting the path was a great help. I tried for hours to get the path using INSTALLDIR, TARGETDIR, and many other variables to no avail. You did a great job in clarifying it all for me. GREAT POST! On another note, my URL is not yet fully functional but will be shortly as I am going to make a piece of freeware available called CiaoVista DesktopLocker. 11/16/2008 7:02 AM | Frank Gennaro

# re: How to get the target directory (TARGETDIR) in a customer windows installer

Gravatar I looked forever for this...this finally solved it. Thank you. 12/4/2008 11:42 AM | Glenn Rogers

# re: How to get the target directory (TARGETDIR) in a customer windows installer

Gravatar Ummm, use a static variable... 12/11/2008 6:45 PM | Shailen Sukul

# re: How to get the target directory (TARGETDIR) in a customer windows installer

Gravatar Very useful, but, is there any way to change that value programmatically, even before the installer presents the user with the box which suggests to select the target directory? I need to display old installation path, when user installs upgrades.

Thanks
Archie
1/20/2009 10:31 PM | Artchil Gogava

# re: How to get the target directory (TARGETDIR) in a customer windows installer

Gravatar Hmmm ... just add the following line to the CustomActionData property of your installer: /name="[TARGETDIR]\"

Use this format to access TARGETDIR and every other parameter passed to the Windows Installer.

Then you can use the following in your installer code Installer.Context.Parameters["TARGETDIR"] ...

See http://msdn.microsoft.com/en-us/library/2w2fhwzz(VS.80).aspx 1/29/2009 3:57 PM | Frank White

# re: How to get the target directory (TARGETDIR) in a customer windows installer

Gravatar Sorry, I meant ...

/TARGETDIR="[TARGETDIR]\" not /name="[TARGETDIR]\"

or you could name it whatever you like :)
1/29/2009 4:31 PM | Frank White

Post a comment





 

 

 

Copyright © Shailen Sukul