安装过程中获取应用程序路径

问题描述 投票:0回答:5

我正在部署一个应用程序,在安装过程中,在用户选择安装应用程序的位置后,我想获取该路径;我已经处于自定义操作中,但我不知道如何获取要安装它的应用程序路径!

它是 Windows 窗体,我正在使用 Visual studio 2010“C#”进行开发。

我正在使用默认的部署工具...

有什么想法吗?

提前致谢...

c# windows winforms setup-deployment
5个回答
45
投票

您的自定义操作所在的类应继承自 System.Configuration.Installer.Installer。它有一个名为 Context 的参数,它有一个参数字典。该字典包含许多有关安装的有用变量,您可以添加一些。

将自定义安装程序添加到“自定义操作”窗格中的安装项目后。选择安装操作并将 CustomActionData 属性设置为:

/targetdir="[TARGETDIR]\"

然后你可以像这样访问路径:

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

1
投票

我知道这是 VB,但这对我有用。

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
投票

很抱歉发布旧帖子的答案,但我的答案可能会帮助其他人。

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);
    }
}

0
投票

字符串 Target_Directory = System.IO.Path.GetDirectoryName(this.Context.Parameters["程序集路径"]);


-1
投票
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
© www.soinside.com 2019 - 2024. All rights reserved.