Geeks With Blogs
Shailen Sukul blog

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

Posted on Wednesday, October 11, 2006 11:48 AM Windows Installer | Back to top


Comments on this post: How to get the target directory (TARGETDIR) in a customer windows installer

# re: How to get the target directory (TARGETDIR) in a customer windows installer
Requesting Gravatar...
It is of great help
Left by Manoj Banga on Jul 20, 2007 7:14 AM

# re: How to get the target directory (TARGETDIR) in a customer windows installer
Requesting Gravatar...
better option then your StripDir(string) method:

String TargetDirectory = Path.GetDirectoryName(Context.Parameters["AssemblyPath"]);
Left by Jason R. Shaver on May 21, 2008 2:32 PM

# re: How to get the target directory (TARGETDIR) in a customer windows installer
Requesting 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.
Left by Frank Gennaro on Nov 16, 2008 7:02 AM

# re: How to get the target directory (TARGETDIR) in a customer windows installer
Requesting Gravatar...
I looked forever for this...this finally solved it. Thank you.
Left by Glenn Rogers on Dec 04, 2008 11:42 AM

# re: How to get the target directory (TARGETDIR) in a customer windows installer
Requesting Gravatar...
Ummm, use a static variable...
Left by Shailen Sukul on Dec 11, 2008 6:45 PM

# re: How to get the target directory (TARGETDIR) in a customer windows installer
Requesting 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
Left by Artchil Gogava on Jan 20, 2009 10:31 PM

# re: How to get the target directory (TARGETDIR) in a customer windows installer
Requesting 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
Left by Frank White on Jan 29, 2009 3:57 PM

# re: How to get the target directory (TARGETDIR) in a customer windows installer
Requesting Gravatar...
Sorry, I meant ...

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

or you could name it whatever you like :)
Left by Frank White on Jan 29, 2009 4:31 PM

# re: How to get the target directory (TARGETDIR) in a customer windows installer
Requesting Gravatar...
Its really great. I search a lot in Google. But your work around worked for me. Thanks a lot.
Left by Pranav Vasa on Jan 28, 2011 7:28 AM

# re: How to get the target directory (TARGETDIR) in a customer windows installer
Requesting Gravatar...
you can use the framework facilities:

string sAssemblyPath = Context.Parameters["AssemblyPath"];
string installDir = System.IO.Path.GetDirectoryName( sAssemblypath );
Left by Wolfbin on May 11, 2011 9:03 PM

# re: How to get the target directory (TARGETDIR) in a customer windows installer
Requesting Gravatar...
Thank you! You really saved me a lot of time.
Left by Sam on Oct 07, 2011 12:37 AM

# re: How to get the target directory (TARGETDIR) in a customer windows installer
Requesting Gravatar...
I tried several ways and still could not find how to set the target directory
Left by Sriram on Oct 15, 2012 9:43 AM

# re: How to get the target directory (TARGETDIR) in a customer windows installer
Requesting Gravatar...
Context.Parameters["AssemblyPath"];
This worked for me. thanks.
But TARGETDIR didn't work don't know the reason tried lot of.
Left by Anoop on Dec 30, 2014 3:20 AM

# re: How to get the target directory (TARGETDIR) in a customer windows installer
Requesting Gravatar...
I agree with Frank White, the property has to be specified exactly like this: /TargetPath="[TARGETDIR]\"
with no spaces. When reading this property using Context.Parameters[targetpath], the path wil contain a trailing '\' that will have to be removed (At least that's the case in VS2013 Setup and Deployment projects).
Left by Cosmin Serban on Jan 07, 2017 8:58 PM

Your comment:
 (will show your gravatar)


Copyright © Shailen Sukul | Powered by: GeeksWithBlogs.net