16

After creating new Visual C# Console Application (.NET Framework 4.5), such project contains default App.config file.

New Visual C# Console Application

After adding a reference for System.Configuration to project and use it in some source file using System.Configuration; I can use static class ConfigurationManager to operate with App.config file. But before, I want to add some settings to the file, so it's somehow like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="DeployWeb" value="true" />
  </appSettings>
  <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

Now, I can write something like this, to get the value of the setting:

Boolean deployWeb = false;
Boolean.TryParse(ConfigurationManager.AppSettings["DeployWeb"], out deployWeb);

However I didn't set which configuration file to read,but it's okay, because there is the default one. But I can add more configuration files by right click on the project -> Add -> New Item... -> Application Configuration File, so that I have, for example, 5 configuration files, like on the picture:

Several configuration files in a project

And ConfigurationManager will still read the default one, but I want to manually control, which config file to use. I want to know, if is there an appropriate way to set to the ConfigurationManager config file name to use etc. and if it's not a bad practice. I know how to use different configurations in debug/release mode, but in my case I have an application, that can be runned in different modes for different purposes in release, for example.

Question: Is it possible to have several configuration files in a project and to have ability to switch which one I want to use. Isn't it a bad practice, shall I use some another approach for my purpose ? Using build events is not suitable in my case (I think).

PS. I am sorry for stretching my question, however my itis quite simple and could be just asked in two sentences, but as the rules says, question should contains details.

UPDATE: From already existing answer "Option: You can use the ConfigurationManager Class to load an alternate config file by code." From reading msdn I didn't get which of the methods should I use. Should I open Exe configuration ?

Community
  • 1
  • 1
prvit
  • 956
  • 3
  • 9
  • 22
  • http://stackoverflow.com/questions/4454016/multiple-app-config-files-in-net-class-library-project – Jonesopolis Jul 03 '14 at 15:15
  • @Jonesy Yes, I saw that question, I've just updated my question, can you take a look.. – prvit Jul 03 '14 at 15:20
  • An explicit reference to `System.Configuration.dll` is required to use `ConfigurationManager` class as this assembly is not referred by default in C# project templates. – RBT Aug 30 '16 at 05:55

1 Answers1

13

It can be done using mapped config file ,

ExeConfigurationFileMap configFileMap = 
        new ExeConfigurationFileMap();
    configFileMap.ExeConfigFilename = "App4.config"; // full path to the config file

    // Get the mapped configuration file
   Configuration config = 
      ConfigurationManager.OpenMappedExeConfiguration( 
        configFileMap, ConfigurationUserLevel.None);

   //now on use config object

AppSettingsSection section = (AppSettingsSection)config.GetSection("appSettings");
RBT
  • 24,161
  • 21
  • 159
  • 240
Ajay Kelkar
  • 4,591
  • 4
  • 30
  • 29