How to make ConfigurationManager read a config file other than app.config?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 01:52:21

问题


I need to parse a config file that is situated in another project. I know that ConfigurationManager reads the app.config file by default, but how to make it read that particular config file?


回答1:


// Create a filemap refering the config file.
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = configFilePath;

// Retrieve the config file.
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);



回答2:


Or like this:

var fileMap = new ConfigurationFileMap(configFilePath);
Configuration config = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);

But you still will have a problem with custom configuration sections.



来源:https://stackoverflow.com/questions/2801411/how-to-make-configurationmanager-read-a-config-file-other-than-app-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!