9

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"]);;
}
Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147

7 Answers7

12

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.

YWE
  • 2,849
  • 28
  • 42
  • 1
    You also have to add an extra space after the backslash "\". Otherwise you may get a FileNotFoundException exception when installing or uninstalling. See [here](http://stackoverflow.com/questions/4516256/how-do-i-fix-setup-project-with-custom-action-file-not-found-exception/4522825#4522825) or [here](http://stackoverflow.com/questions/8491095/error-1001-using-custom-installer-with-visual-studio-2008). – Masood Khaari Aug 11 '13 at 08:24
  • Where are you using this bit of text? Your explanation is not quite clear to me, sorry. As a command line argument for the project? Somewhere in code? That link to MSDN is broken now, too. Thanks Microsoft! :-) – Dan Csharpster May 15 '14 at 22:54
  • Oh, I see now. I found the answer in another post: "In your deployment project, add a Custom Action for Install and Commit. For the CustomActionData property for the Install custom action, enter /Targetdir="[TARGETDIR]\" " – Dan Csharpster May 15 '14 at 22:56
3

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.

LanceSc
  • 2,094
  • 12
  • 14
  • Is WiX capable of installing Windows Services? – Spencer Ruport Feb 19 '10 at 01:37
  • Yes WiX is capable of installing services. You just need to author the ServiceInstall Element, http://wix.sourceforge.net/manual-wix2/wix_xsd_serviceinstall.htm. WiX and Visual Studio 2008 setup projects are both just front ends for MSI. If you decide to go with WiX I would suggest reading, http://msdn.microsoft.com/en-us/library/aa370566(VS.85).aspx and all it's sub entries. It will help you understand components and features. I originally started using Visual Studio 2003\2005 for our installs and just ran into too many limitations. – LanceSc Feb 19 '10 at 04:01
  • I had to stick with a Visual Studio installation project for this project but +1 for giving me a viable alternative to research in the future. :) – Spencer Ruport Mar 26 '10 at 17:25
2

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.

jonsca
  • 10,218
  • 26
  • 54
  • 62
Evan
  • 21
  • 1
1

For those who have multiple will have to do like this

/AppID="[APPID]" /Path="[TARGETDIR]\"
Chief
  • 914
  • 1
  • 9
  • 26
0

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.

Regina
  • 41
  • 1
0

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" ];
Berg
  • 350
  • 1
  • 2
  • 8
-1

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.

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147