ConfigurationManager.AppSettings use another config file

左心房为你撑大大i 提交于 2019-11-27 18:21:20

问题


I have about 10 methods in my class. In every method I use ConfigurationManager.AppSettings to get value form App.config file

like

 _applicationPort = int.Parse(ConfigurationManager.AppSettings["ApplicationPort"]

My problem is that I want to make this code get AppSettings from another app.config file like AnotherPoject.exe.config.


回答1:


You can also set the app.config to read another file. Something like this:

<?xml version="1.0"?>
<configuration>
  <appSettings  file="my\custom\file\path\external.config"/>
</configuration>

and the external.config will have the appSettings section:

<appSettings>
    <add key="myKey" value="myValue" />
</appSettings>

refer to this msdn for additional info.




回答2:


You could do something like this

var fileConfig = ConfigurationManager.OpenExeConfiguration("<filePath>");
int port = int.Parse(fileConfig.AppSettings["PortNumber"].ToString());



回答3:


You can accomplish this by using ConfigurationManager.OpenExeConfiguration. This will allow you to open another configuration file easily.

MSDN article about OpenExeConfiguration.



来源:https://stackoverflow.com/questions/16425407/configurationmanager-appsettings-use-another-config-file

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