如何从另一个程序集中读取 app.config? [英] How to read app.config from another assembly?

查看:53
本文介绍了如何从另一个程序集中读取 app.config?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个项目:

  • 控制台项目 (Test.exe)
  • 类库项目 (Test.Data.dll)

我的类库项目包含一个 app.config 文件.

My Class Library project contains an app.config file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="TestEntities" connectionString="metadata=res://*/DBNews.csdl|res://*/DBNews.ssdl|res://*/DBNews.msl;provider=System.Data.SqlClient;provider connection string=&quot;{0}&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

我想从控制台项目访问类库中的设置,所以我尝试过:

From the Console project I want to access the settings from the Class Library, so I've tried:

var config = ConfigurationManager.OpenExeConfiguration("Test.Data.dll");
config.ConnectionStrings.ConnectionStrings[0].Name; // LocalSqlServer
// seems to be the wrong assembly.

还有:

var config = ConfigurationManager.OpenExeConfiguration("Test.Data.dll.config");
// invalid exePath

如何访问 DLL 的 app.config ?

How can I access the DLL's app.config ?

推荐答案

DLL 在运行时没有自己的 app.config.app.config 仅适用于实体框架设计器.

The DLL doesn't have its own app.config at runtime. The app.config is only there for the Entity Framework designer.

在执行期间,DLL 将尝试从应用程序的 app.config 文件中读取值.对于实体框架连接,这意味着您必须将连接信息复制到应用程序的 app.config 中.

During execution, the DLL will try to read the values from the Application's app.config file. For Entity Framework connections, that means you have to copy the connection information into the Application's app.config.

这篇关于如何从另一个程序集中读取 app.config?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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