简体   繁体   中英

Getting Application path during the installation

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...

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.
    } 
}

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

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);
    }
}
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM