Spencer Ruport
Spencer Ruport

Reputation: 35107

Visual Studio Install Project: Get Target Directory

I've been digging around Google trying to find the appropriate way to determine the installation path selected by a user from the install wizard.

Basically I'm running into an issue where my service can't create files in it's own directory because it lacks the proper permissions. I'm assuming the correct way to resolve this is to make sure that whatever account the service is using is given appropriate file permissions on it's folder.

But before I can even tackle how to set permissions through .Net I need to know the installation folder. I'm using an install project which has an Installer class which contains a ServiceInstaller control as well. Both have the Context property so I've been checking that for the parameters that are available when the AfterInstall event fires for each of the respective installers. I thought at first I'd be seeing the TargetDir property set but that hasn't been the case. I am however seeing AssemblyPath set and pointing to the executable of the appropriate folder.

Essentially I just want to make sure that this is the appropriate method I should be using:

private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
{
    string InstallPath = System.IO.Path.GetDirectoryName(serviceInstaller1.Context.Parameters["AssemblyPath"]);;
}

Upvotes: 10

Views: 16925

Answers (7)

Evan
Evan

Reputation: 21

During the install, event I did this:

// locate the installation directory and store it where we can find it during Commit
stateSaver.Add("TargetDir", Context.Parameters["DP_TargetDir"]);

then, I was able to access TargetDir later during OnCommitted:

string path = (string)savedState["TargetDir"];

Not sure if that helps or not! I'm trying to figure out how to reliably determine the install directory so that my service can do some logging.

Upvotes: 2

Chief
Chief

Reputation: 914

For those who have multiple will have to do like this

/AppID="[APPID]" /Path="[TARGETDIR]\"

Upvotes: 1

YWE
YWE

Reputation: 2909

I found that the solution that Berg gave works for me except using this value for the CustomActionData property:

/TargetDir="[TARGETDIR]\"

Note the addition of the backslash. See this article on MSDN.

Upvotes: 12

Berg
Berg

Reputation: 350

To get the target directory property value in your custom action you can forward it manually by selecting your custom action output in the custom action view and putting something like:

/TargetDir="[TARGETDIR]"

as the value for the CustomActionData property.

You should then be able to access it by:

string targetDir = Context.Parameters[ "TargetDir" ];

Upvotes: 0

Spencer Ruport
Spencer Ruport

Reputation: 35107

As far as I can tell this is the only way to determine the install directory. Of course I'll take note if someone comes along with a different answer but until then this is the approach I'm taking.

Upvotes: -1

Regina
Regina

Reputation: 41

As an alternative to setup projects, you can use some installer building services. I think, with http://installer.codeeffects.com you can load any files from your website and put them in installation directory when user installs your service. Hope this helps.

Upvotes: 0

LanceSc
LanceSc

Reputation: 2124

Your custom action is a deferred custom action and only certain properties are available to it, see the following page for more details, http://msdn.microsoft.com/en-us/library/aa370543(VS.85).aspx. You may be able to add the TARGETDIR property to the CustomActionData in Visual Studio 2008; however, I have not worked with Visual Studio 2008 as an authoring tool.

Doing complicated installs in Visual Studio 2008 is very difficult because it abstracts away a number of key features of MSI. I would strongly suggest taking a look at WiX.

Even if you don't use WiX, you will want to download Orca, http://msdn.microsoft.com/en-us/library/aa370557(VS.85).aspx and use it to validate your install. This will save you countless hours later.

Upvotes: 3

Related Questions