安装项目、自定义安装程序、连接字符串 [英] Setup project , Custom Installer, Connection string

查看:71
本文介绍了安装项目、自定义安装程序、连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个安装项目.在一个单独的库项目中,我通过继承 System.Configuration.Install.Installer 创建了一个自定义安装程序,并将生成的 .dll 添加为自定义操作.(安装步骤).我在库项目中添加了一个 app.config 文件,我在其中存储了连接到 Sql Server 所需的连接字符串.

I'm working on a setup project. In a separate library project I created a custom installer by inheriting from System.Configuration.Install.Installer and added the generated .dll as a custom action.(Install step). I added an app.config file to the library project where I store a connection string which I need to make a connection to Sql Server.

一旦我运行安装项目,自定义安装程序就不会检索存储在 app.config 文件中的 connectionString.

Once I run the setup project, the custom installer doesn’t retrieve the connectionString stored in the app.config file.

在哪里可以存储连接字符串?安装项目可以有 app.config 吗?有人可以推荐一本关于部署/设置项目的书吗?

Where can I store the connection string? Can a setup project have an app.config? Could somebody recommend a book on deployment/setup projects?

谢谢

更新

谢谢.根据回复,我更新了我的代码,这就是我现在正在做的:

Hi, Thanks. Based on the replies I've updated my code this is what I´m doing now:

-> 在安装项目中,我在安装步骤中添加了一个自定义操作,选择应用程序文件夹和主要输出(库项目).-> 向库项目添加了一个 app.config 并将其构建操作设置为内容".-> 将库项目内容文件添加到安装项目中.

-> In setup project, I added a custom action to the install step, selecting application folder and primary output(library project). -> Added an app.config to the library project and set its build action to "content". -> Added the library project content files to the setup project.

这样 app.config 文件就会出现在安装文件夹中.

This way the app.config file appears in the install folder.

在我的自定义安装类的安装处理程序中,我执行以下操作:(在这种情况下,我访问应用程序设置)

Within the install handler from my custom install class I do the following: (in this case I access the application settings)

           string appPath = "";
        appPath = Path.Combine(new DirectoryInfo(Context.Parameters["assemblypath"].ToString()).Parent.FullName, "App.config");

        ExeConfigurationFileMap map = new ExeConfigurationFileMap();
        map.ExeConfigFilename = appPath;
        System.Configuration.Configuration c = null;
        c = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
        string result = c.AppSettings.Settings["test"].Value;

推荐答案

如果你坚持这样做,你仍然可以重用 app.config,但你需要想出你自己的解析方式.

You can still reuse app.config if you insist on doing so, but you need to come up with your own way to parse it.

以下是此解决方法的必要步骤:

Here is the necesary steps for this workaround:

  1. 创建自己的机制来使用读取 appSettingsSystem.Xml.XmlReader 或任何来自app.config 文件将是复制到应用程序文件夹中.
  2. 使用您的自定义 xml 解析器来提取值并将其用于您的自定义操作.
  3. 标记App.config 作为构建操作属性中的内容窗口(在自定义操作项目中).
  4. 在安装项目中,在应用程序文件夹中添加项目输出并选择内容文件而不是主要输出.你可以验证 app.config 是输出通过调用上下文菜单并选择内容文件的输出来自... 物品.

  1. Create your own mechanism to read the appSettings using System.Xml.XmlReader or whatever from the app.config file that will be copied into the Application Folder.
  2. Use your custom xml parser to extract the value and use it in your custom action.
  3. Mark App.config as Content in the Build Action property window (in the custom action project).
  4. In the Setup Project, add Project Output in the Application Folder and choose Content Files instead of Primary output. You can verify that app.config is the output by invoking context menu and choosing Outputs for the Content Files from ... item.

完成此操作后,您应该有来自自定义安装程序类库项目的 2 个输出(1 个用于主输出,1 个用于内容文件).

以下是一个示例自定义安装程序操作,它将启动我在 appSettings 键中名为 launch 的任何内容以及我的替换 ConfigurationManager 实现:

Here is an example custom installer action that will launch whatever I have in my appSettings key called launch along with my replacement ConfigurationManager implementation:

using System;
using System.Configuration.Install;
using System.Xml;
using System.IO;
using System.Reflection;
using System.ComponentModel;
using System.Collections;

namespace ClassLibrary1
{
    [RunInstaller(true)]
    public partial class Installer1 : System.Configuration.Install.Installer
    {
        public Installer1()
        {
            InitializeComponent();
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
            System.Diagnostics.Process.Start(ConfigurationManager.AppSettings["launch"]);
        }
    }

    public class ConfigurationManager
    {
        private static AppSetting _appSettings = new AppSetting();

        public static AppSetting AppSettings
        {
            get { return _appSettings; }
        }

        public class AppSetting
        {
            public string this[string key]
            {
                get
                {
                    var path = Path.Combine(Path.GetDirectoryName(Assembly.GetCallingAssembly().Location), "app.config");
                    var xpath = string.Format("/configuration/appSettings/add[@key='{0}']", key);
                    var doc = new XmlDocument();

                    doc.Load(path);
                    var node = doc.SelectSingleNode(xpath);

                    if (node == null) 
                        return string.Empty;

                    return node.Attributes["value"].Value;
                }
            }
        }
    }
}

这篇关于安装项目、自定义安装程序、连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆