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

标签 c# setup-project

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

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

我在哪里可以存储连接字符串?安装项目可以有 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. 创建你自己的机制 使用读取 appSettings System.Xml.XmlReader 或任何来自 将是的 app.config 文件 复制到应用程序文件夹中。
  2. 使用您的自定义 xml 解析器 提取值(value)并将其用于您的 自定义操作。
  3. 马克 App.config 作为 Build Action 属性中的内容 窗口(在自定义操作项目中)。
  4. 在安装项目中,在应用程序文件夹中添加项目输出并选择内容文件 而不是主要输出。你可以 验证 app.config 是输出 通过调用上下文菜单并选择 内容文件的输出来自 ... 元素。

    完成此操作后,您应该从自定义安装程序类库项目中获得 2 个输出(1 个用于主输出,1 个用于内容文件)。

这是一个示例自定义安装程序操作,它将启动我在名为 launch 的 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;
                }
            }
        }
    }
}

关于c# - 安装项目,自定义安装程序,连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5030416/

相关文章:

c# - ASP.NET MVC 代码优先 : Multiplicity is not valid in Role in relationship

c# - 如何在 visual studio 2010 安装项目中设置 "run as administrator"特权?

c# - 如何在C#中获取unix时间戳

c# - C# 应用程序中 where 子句中的未知列

c# - 安装/部署项目 : Prevent modified files from being removed when uninstalling

c# - 为 Visual Studio 安装项目创建自定义开始菜单快捷方式链接

java - Eclipse 将带有 jar 的项目添加到新项目中

C# 安装项目自定义操作将安装目录保存到自定义设置文件

c# - 如何读取本地cookie

c# - 我怎样才能把这个类写得完全通用,并根据一个请求返回不同的响应?