posts - 18, comments - 23, trackbacks - 1

My Links

News

This blog has moved to http://shailen.sukul.org

Article Categories

Archives

Post Categories

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

Print | posted on Wednesday, October 11, 2006 11:48 AM | Filed Under [ Windows Installer ]

Feedback

Gravatar

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

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

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

better option then your StripDir(string) method:

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

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

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
Gravatar

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

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

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

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

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

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
Gravatar

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

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
Gravatar

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

Sorry, I meant ...

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

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

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

Its really great. I search a lot in Google. But your work around worked for me. Thanks a lot.
1/28/2011 7:28 AM | Pranav Vasa
Gravatar

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

you can use the framework facilities:

string sAssemblyPath = Context.Parameters["AssemblyPath"];
string installDir = System.IO.Path.GetDirectoryName( sAssemblypath );
5/11/2011 9:03 PM | Wolfbin
Gravatar

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

Thank you! You really saved me a lot of time.
10/7/2011 12:37 AM | Sam
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: