2011-02-17 96 views
1

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

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

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

感谢

UPDATE

嗨, 感谢。根据我更新我的代码的回复,这是我现在正在做的:

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

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

内我的自定义安装处理器安装I类做到以下几点:(在这种情况下,我访问的应用程序设置)

  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; 
+0

将其存储为一个常量字符串,或者可能作为嵌入式资源。一个app.config属于一个.exe。你没有一个.exe。 – 2011-02-17 15:03:11

回答

4

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

以下是此变通方法necesary步骤:

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

    完成此操作后,您应该有2个来自Custom Installer类库项目的输出(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; 
       } 
      } 
     } 
    } 
} 
1

,而不是试图解析app.config,我将建议将configSource属性添加到您的app.config以外化连接字符串,例如:

<connectionStrings configSource="connections.config" /> 

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

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

确保您使用.config作为文件的扩展名,因此不能提供给浏览器,如果有人猜出了文件名。