代码之家  ›  专栏  ›  技术社区  ›  Pat

使用已安装应用程序路径的自定义.NET安装程序的简单示例

  •  5
  • Pat  · 技术社区  · 15 年前

    我只想创建一个自定义安装程序,在安装后运行代码,这需要安装的应用程序的路径。

    我读到有关 how to create a custom installer Custom Actions 以及什么 properties are available in the installer 但我不太明白如何从自定义安装程序代码内部访问这些属性。(甚至不要让我开始了解 Windows Installer documentation )

    最好的答案是使用应用程序路径的自定义安装程序的完整代码。这就是我到目前为止所得到的:

    using System;
    using System.ComponentModel;
    
    namespace Hawk
    {
        [RunInstaller(true)]
        public class Installer : System.Configuration.Install.Installer
        {
            public Installer()
            {
    
            }
    
            public override void Install(System.Collections.IDictionary stateSaver)
            {
                base.Install(stateSaver);
    
                try
                {
                    //TODO Find out installer path
                    string path = (string)stateSaver["TARGETDIR"]; // Is this correct?
                    // Environment.CurrentDirectory; // What is this value?
                    MyCustomCode.Initialize(path);
                }
                catch (Exception ex)
                {
                    // message box to show error
                    this.Rollback(stateSaver);
                }
            }
        }
    
    }
    
    1 回复  |  直到 15 年前
        1
  •  3
  •   Pat    15 年前

    我想我必须自己做每件事(叹气);-)

    using System;
    using System.ComponentModel;
    using System.IO;
    
    namespace Hawk
    {
        [RunInstaller(true)]
        public class Installer : System.Configuration.Install.Installer
        {
            public override void Install(System.Collections.IDictionary stateSaver)
            {
                base.Install(stateSaver);
                try
                {
                    string assemblyPath = this.Context.Parameters["assemblypath"];
                    // e.g. C:\Program Files\[MANUFACTURER]\[PROGRAM]\[CUSTOM_INSTALLER].dll
                    MyCustomCode.Initialize(assemblyPath);
                }
                catch (Exception ex)
                {
                    //TODO message box to show error
                    this.Rollback(stateSaver);
                }
            }
        }
    }
    
    推荐文章