【发布时间】:2009-02-10 21:51:08
【问题描述】:
我需要从引用的程序集中访问 Web 应用程序的 web.config。我需要获取文件的路径或配置对象。我无法使用 System.Reflection.Assembly.GetEntryAssembly 来执行此操作,就像我为应用程序配置 windows exe 所做的那样。
谢谢
【问题讨论】:
标签: asp.net assemblies web-config
我需要从引用的程序集中访问 Web 应用程序的 web.config。我需要获取文件的路径或配置对象。我无法使用 System.Reflection.Assembly.GetEntryAssembly 来执行此操作,就像我为应用程序配置 windows exe 所做的那样。
谢谢
【问题讨论】:
标签: asp.net assemblies web-config
我在我的类库中使用以下代码从配置文件中读取:
using System.Configuration;
...
string value = ConfigurationManager.AppSettings.Get("myKey");
// returns null if the specified key does not exist
这可以读取 Web 应用程序 (web.config) 和 Windows 窗体/控制台应用程序 (application.exe.config) 中的应用程序设置。
【讨论】:
您可以使用 System.Configuration 程序集中的 ConfigurationManager 类。
但是你不会得到路径,因为配置是很多配置文件的混合(machin.config,几个web.config等)
【讨论】:
这不是一个好的做法,因为它把太多东西结合在一起了。
相反,将所需的配置设置从宿主程序集传递到它引用的任何程序集。
通过这种方式,可以在不依赖配置文件的情况下构建和测试引用的程序集。
【讨论】: