16

I'm deploying an application and during the installation after the user chooses where to install the app, I want to get that path; I'm in a custom action already but i don't know how to get the application path where it's going to be installed !

It's Windows Forms and I'm developing using Visual studio 2010 "C#".

And I'm using the default deploying tool...

Any idea?

thanks in advance...

entryton
  • 280
  • 5
  • 18
Stacker
  • 8,157
  • 18
  • 73
  • 135
  • Based on "custom action" I'm guessing you're using Windows Installer. Are you using Wix or a Visual Studio Setup Project? –  Oct 13 '10 at 15:19
  • oh im sorry i forgot to provide more info i will edit my question... – Stacker Oct 13 '10 at 15:20

4 Answers4

45

The class your custom action is in should inherit from System.Configuration.Installer.Installer. This has a parameter on it called Context which has a Parameters dictionary. The dictionary contains a number of useful variables about the install and you can add some.

Once you have added the custom installer to your install project in the Custom Actions pane. Select the Install action and set the CustomActionData property to:

/targetdir="[TARGETDIR]\"

Then you can access the path like this:

[RunInstaller(true)]
public partial class CustomInstaller : System.Configuration.Install.Installer
{
    public override void Install(System.Collections.IDictionary stateSaver)
    {
        base.Install(stateSaver);
        string path = this.Context.Parameters["targetdir"]; 
        // Do something with path.
    } 
}
superjos
  • 12,189
  • 6
  • 89
  • 134
Martin Brown
  • 24,692
  • 14
  • 77
  • 122
  • 1
    My bad. You also need to set the CustomActionData property. Post updated. – Martin Brown Oct 13 '10 at 17:28
  • How to use TARGETDIR in visual c++? – karikari Feb 22 '11 at 06:19
  • 2
    That trailing slash was making me angry... Still is. Turns out the `"[TARGETDIR]\"` is a quoting problem in the compile process. (Says some guy on the internet.) http://social.msdn.microsoft.com/Forums/en-US/winformssetup/thread/4c71d619-5322-4734-89f3-5c1997e42b93/ – teynon Apr 22 '13 at 20:49
  • Thank you.Recently i'd like to invoke **regsvr32** command in the program, it keeps appearing "fail to load "C:\Program " module".The reason is you have to add _double_ _quote_ around the path,like `path = "\"" + path+"\""`;Cheers! – zionpi Jun 28 '13 at 05:26
  • Still works under VS 2015. If i find the time i will create Videos showing how it's done. – Squirrel in training Sep 28 '16 at 11:41
  • [example of editing the CustomActionData in the properties window](https://imgur.com/a/NTJtARs) – dtwk2 Nov 23 '21 at 09:41
1

I know it's VB but This worked for me.

Private Sub DBInstaller_AfterInstall(ByVal sender As Object, ByVal e As   System.Configuration.Install.InstallEventArgs) Handles Me.AfterInstall

    MessageBox.Show(Context.Parameters("assemblypath"))

 End Sub
0

Sorry to post answer for old post but my answer may help other.

public override void Install(System.Collections.IDictionary stateSaver)
{
    base.Install(stateSaver);
    rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
    if (rkApp.GetValue("MyApp") == null)
    {
        rkApp.SetValue("MyApp", this.Context.Parameters["assemblypath"]);
    }
    else
    {
        if (rkApp.GetValue("MyApp").ToString() != this.Context.Parameters["assemblypath"])
        {
            rkApp.SetValue("MyApp", this.Context.Parameters["assemblypath"]);
        }
    }
}

public override void Uninstall(System.Collections.IDictionary savedState)
{
    base.Uninstall(savedState);
    rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

    if (rkApp.GetValue("MyApp") != null)
    {
        rkApp.DeleteValue("MyApp", false);
    }
}
Mou
  • 15,673
  • 43
  • 156
  • 275
-1
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Slate
  • 3,189
  • 1
  • 31
  • 32
  • 1
    This question asks about working with an installer. Not how to get the path of the currently executed program. – BDL Jan 25 '19 at 13:24
  • @BDL No, the question asks the path of the target application being installed. For a Windows Services installer, the answer is the executing assembly as I provided. – Slate Jan 25 '19 at 14:43