繁体   English   中英

安装项目,自定义安装程序,连接字符串

[英]Setup project , Custom Installer, Connection string

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

运行安装项目后,自定义安装程序将不会检索存储在app.config文件中的connectionString。

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

谢谢

更新

你好谢谢。 根据答复,我更新了代码,这是我现在正在做的事情:

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

这样,app.config文件将出现在安装文件夹中。

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

           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,仍然可以重用,但是您需要提出自己的解析方法。

这是此替代方法的必要步骤:

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

    完成此操作后,您应该从Custom Installer类库项目中获得2个输出(1个代表主输出,1个代表内容文件)。

这是一个示例自定义安装程序操作,该操作将启动我在appSettings项中称为“启动”的所有内容,以及我的替代ConfigurationManager实现:

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

与其尝试解析app.config ,不建议将configSource属性添加到app.config以使连接字符串外部化,例如:

<connectionStrings configSource="connections.config" />

然后获取安装脚本以生成整个connections.config文件,类似于:

<connectionStrings>
  <clear />
  <add name="...." connectionString="...." providerName="...." />
</connectionStrings>

确保使用.config作为文件的扩展名,因此,如果有人猜到了文件名,则无法将其提供给浏览器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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