Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConfigurationManager.AppSettings use another config file

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.

like image 761
Tarek Saied Avatar asked May 07 '13 17:05

Tarek Saied


3 Answers

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.

like image 189
Tobia Zambon Avatar answered Oct 29 '22 14:10

Tobia Zambon


You could do something like this

var fileConfig = ConfigurationManager.OpenExeConfiguration("<filePath>");
int port = int.Parse(fileConfig.AppSettings["PortNumber"].ToString());
like image 32
Bearcat9425 Avatar answered Oct 29 '22 15:10

Bearcat9425


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

MSDN article about OpenExeConfiguration.

like image 36
Justin Avatar answered Oct 29 '22 15:10

Justin