12

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";
Praveen
  • 55,303
  • 33
  • 133
  • 164

1 Answers1

28

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).

Mike
  • 6,149
  • 5
  • 34
  • 45
  • Should I have this line in `app.config.exe`? – Praveen Oct 07 '12 at 13:58
  • Have edited and fixed up - see if you can get that working? Is it appSettings you are trying to move into another file, or some other config section? Either way, the same principle applies. – Mike Oct 07 '12 at 14:14
  • @Aniket Done. sorry, I missed to pick it on the right time. – Praveen Mar 25 '15 at 04:48
  • 3
    Please note that the second config (_accessLevel.config_) begins with `` element, not default `` element. I spend about 10 minut before i realize this ;) – Jan 'splite' K. Apr 17 '15 at 09:24
  • How is this "reading values from multiple Configuration files"? Only values from accessLevel.config are read, via app.config... – Erik Nov 14 '20 at 14:40
  • Because there are 2 (multiple) configuration files involved, as you identified. This was the solution to the OP’s question, allowing them to keep the accessLevel config in its own file, but read its values as if they came from app.config. – Mike Nov 15 '20 at 19:07