[英]multiple config file in c#
我有一个包含1个控制台应用程序和2个库的解决方案。
在库中,我有两个不同的app.configs作为示例我的data.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="OutputFileFolder" value="c:\\log" />
<add key="OutputIndexFile" value="c:\\log\index.xml" />
</appSettings>
</configuration>
在这个库类中,我在构造函数中
_indexPath = ConfigurationManager.AppSettings["OutputIndexFile"];
但是我应该如何从主控制台应用程序加载Data.config
文件(这应该是主配置文件)?
库实际上没有关联的配置文件,它们在可执行文件(在您的情况下为控制台应用程序)下运行。
您应该将所有配置都放在应用程序的app.config
文件中,以便库中的代码可以访问它。
通过有多个可以加载多个配置文件Configuration
多个调用实例ConfigurationManager.OpenMappedExeConfiguration
(添加您的配置文件到文件的地图,全球文件将被自动添加,并指定一个ConfigurationUserLevel.None
。
就像是:
var fileMap = new ExeConfigurationFileMap {
ExeConfigFilename = Path of dll's config file
};
var cfg = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
var result = cfg.AppSettings["OutputIndexFile"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.