views:

1312

answers:

5

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"]);;
}
+1  A: 

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
Is WiX capable of installing Windows Services?
Spencer Ruport
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
@LanceSc: 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
A: 

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
A: 

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
A: 

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
As I said in my question, I checked that value but unfortunately it is not set at the time the event fires.
Spencer Ruport
+1  A: 

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
This is helpful thank you.
Spencer Ruport