Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read values from multiple Configuration file in c# within a single project?

Here in my project I have two application configuration files called app.config and accessLevel.config. Now using the OpenExeConfiguration I was able to access the app.config.exe file but not the accessLevel.config. Please help on this.

The main reason I have 2 config files is to show the difference and make the code simple. I need to read the values from the accessLevel.config in my C# code.

Tried the below code but no use:

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
config.AppSettings.File = "App2.config";
like image 789
Praveen Avatar asked Oct 07 '12 13:10

Praveen


People also ask

Can we have multiple config files?

config file in sub root folders we can use multiple web. config files in our application but each folder should contains only one web. config file.

Can we have multiple config file for single project?

Yes you can have two web. config files in application. There are situations where your application is divided in to modules and for every module you need separate configuration.

What is a .conf file?

In computing, configuration files (commonly known simply as config files) are files used to configure the parameters and initial settings for some computer programs. They are used for user applications, server processes and operating system settings.

Is conf and CFG same?

There's no particular meaning. Both are short for "configuration".


1 Answers

See here.

Put this in your App.config:

<appSettings file="accessLevel.config"/>

And then have another file called accessLevel.config like this:

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
  <add key="TestSetting" value="TestValue"/>
</appSettings>

And then you can access your config values in code like this:

string value = ConfigurationManager.AppSettings["TestSetting"];

Make sure that accessLevel.config is set to copy to the output directory (right click the file in Visual Studio -> Properties -> Copy To Output Directory -> Copy if Newer).

like image 114
Mike Avatar answered Oct 05 '22 04:10

Mike