After creating new Visual C# Console Application (.NET Framework 4.5), such project contains default App.config file.
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:
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 ?
You cannot use multiple configuration files (i.e. one per library project) without coding.
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.
There is no restriction to use the web. config file in the asp.net web application. You can have 1 Web. config file per folder .
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With