如何使DLL具体配置文件? [英] How to have DLL specific config file?

查看:133
本文介绍了如何使DLL具体配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在我的解决方案中有多个配置文件。例如,对于我的控制台应用程序(Program.Main),我想为任何设置引用app.config文件。虽然我的控制台应用程序内部调用了一个类库Dll1的方法。在Dll1里面我想要代码从Dll1.config中进行一些设置。同样,我的控制台应用程序也调用了一个Dll2的方法。在Dll2里面我想让代码从Dll2.config文件中进行设置。



请帮忙解决这个问题。还有,是否有可能?
如果您可以提供一个小的代码示例,我将非常有帮助。

解决方案

是的,这是可能的。就像你可以有exe.config文件,它完全正常有一个dll.config文件。在其配置文件中存储DLL特定信息。稍后,您可以通过以下代码访问DLL中的配置信息:

  var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly())。位置); 
string dllConfigData = appConfig.AppSettings.Settings [dllConfigData]。


I wish to have multiple config files in my solution. For example, For my console application (Program.Main) I want to refer app.config file for any settings. While my console application internally calls a method of a class libray Dll1. Inside Dll1 I want the code to take some settings from Dll1.config. Similarly my Console application also calls a method of Dll2. Inside Dll2 I want the code to take settings from Dll2.config file.

Please help on how to achieve this. Also, is it possible or not? I would be really helpful if you could provide with a small code sample.

解决方案

Yes, it is possible. Just like you can have exe.config file, its perfectly normal to have a dll.config file. Store DLL specific information in its config file. Later on you can access this configuration information from DLL by following code

var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
string dllConfigData = appConfig.AppSettings.Settings["dllConfigData"].Value;

这篇关于如何使DLL具体配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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