How to have different parts of a configuration file in C #

what I'm trying to do is for my App.config file. I have many settings, and I want to split my configuration file into different files. For example; my app.config file has email-related settings, so I want to extract these settings and save them in the email.config file and then use the configSource attribute in my app.config file to add the thos settings from the email file. config file and add it to the node application settings. Is it possible?

If so, consult on how to achieve the above result.

Many thanks.

so for example, I have another configuration file called app1.config and has the following xml:

<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings > <add key="l" value="test"/> </appSettings> </configuration> 

and then from my main app.config file they refer to the app1.config file, and then from the code you can do this to get the value of the application installation key:

  var x = ConfigurationManager.AppSettings["l"]; 
+7
source share
4 answers

EDIT to reflect the modified question and additional comments:

For user settings defined in the <appSettings> configuration file, there is a file attribute, which can contain a path to a file that overrides appSettings: http://www.codeproject.com/Articles/8818/Using-the-File-attribute -of-the-appSettings-elemen

 <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings file="settings.config"> </appSettings> </configuration>` 

You can also use the configSource attribute, as indicated in the MSDN documentation:

http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource.aspx

The ConfigSource property represents the value of the configSource attribute specified for the ConfigurationSection object associated with the SectionInformation object.

In the implementation of ConfigurationSection, you can specify a separate file in which the configuration parameters for this section are defined. This can be useful in several ways:

Using include files can lead to a more logical and modular structure of configuration files.

File protection and permissions can be used to restrict access to configuration parameter sections.

Settings in the included file that are not used when initializing the application can be changed and reloaded without having to restart the application.

The following example shows how this attribute is used in a configuration file to indicate that a page section is defined in an external include file:

<pages configSource="pages.config"/>

Or, if you want to store information from the same section in separate files, you can always return to using ConfigurationManager.Open ... Configuration functions and programmatically read parameters: http://msdn.microsoft.com/en-us/library/ ms134262.aspx

+6
source

You can use the built-in smtp settings configuration section in your own file:

 <system.net> <mailSettings> <smtp deliveryMethod="network"> <network host="localhost" port="25" defaultCredentials="true" /> </smtp> </mailSettings> </system.net> 

This can be specified in app.config using configSource .

You can limit this only to the smtp section if you want:

 <system.net> <mailSettings> <smtp configSource="smtp.config" /> </mailSettings> </system.net> 
+4
source

You can do this using the configSource attribute:

  <?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings configSource="cs.config" /> </configuration> 
0
source

ConfigSource maps the entire section to an external file. Once you have added it, you can no longer use the section in the root configuration file. You can also have more than one mapping for each section.

You can create a custom configuration section, as suggested above, with the sections you want to display, and then map each section one at a time.

web.config:

 <myConfig> <mysection1 file="section1.config"/> <mysection2 file="section2.config"/> </myConfig> 

section1.config:

 <mysection1> <add key="key1" value="val1"/> </mysection1> 
0
source

All Articles