Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include a config file for .Net

Is there a way to have the main .net configuration file app.config/web.config include another configuration file? I have a need to keep things in separate files, but link them together.

Here is a sample of what I want to include:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="LocationSearch.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <LocationSearch.Properties.Settings>
        <setting name="MapQuestApiKey" serializeAs="String">
        <value>some value here...</value>
        </setting>
        <setting name="MapQuestClientCode" serializeAs="String">
        <value>another value here...</value>
        </setting>
    </LocationSearch.Properties.Settings>
  </applicationSettings>
</configuration>
like image 312
Paul Fryer Avatar asked Sep 03 '10 17:09

Paul Fryer


People also ask

Where is .NET config file?

The Machine. config file is located in the %SystemRoot%\Microsoft.NET\Framework\%VersionNumber%\CONFIG\ directory.

How do I add a config file to a .NET core console application?

In the Main method, add the following code: class Program { static void Main(string[] args) { //.... IConfiguration Config = new ConfigurationBuilder() . AddJsonFile("appSettings.


2 Answers

Yes, with the 'file' attribute

<appSettings file="configFiles/otherConfigFile.config">

MSDN info

Some relevant information from the MSDN article about using the 'file' attribute:

The contents of the separate file are merged with the appSettings section in the Web.config file. This functionality is limited to the appSettings attribute.

Note: In the .NET Framework version 2.0, you can now include configuration settings in a separate file for all configuration elements that support the configSource attribute. However, when you use the configSource attribute, you must move the entire section to the separate file because there is no merging of element settings.

like image 149
Jim Ecker Avatar answered Oct 22 '22 23:10

Jim Ecker


Yep, you can include separate files into the main .config using the configSource attribute like so:

<securityConfiguration configSource="SomeOtherConfigFile.config" />

External config files that you include this way must be within the same directory (or a subfolder) of the main .config file.

SectionInformation.ConfigSource Property

like image 41
Kevin Tighe Avatar answered Oct 23 '22 00:10

Kevin Tighe