2

I have a custom .NET addin for an application and I am trying to create configSections for the addin's config file. The trouble is I am not able to read that section If load the configuration using the OpenMapperExeConfiguration/OpenExeConfiguration.

Here is my config file(MyTest.dll.config)

<configuration>
  <configSections>
    <section name="test" type="MyTest, Test.ConfigRead"/>
    </configSections>
    <test>
            ..Stuff here
        </test>
    <appSettings>
        <add key="uri" value="www.cnn.com"/>
    </appSettings>
</configuration>

Here is my code sample to access the test configSection

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();  
fileMap.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + "config";    
Configuration applicationConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap,ConfigurationUserLevel.None);
//Using OpenExeConfiguration doesnt help either.
//Configuration applicationConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
//Accessing test section
applicationConfig.GetSection("test");

//Accessing AppSettings works fine.
AppSettingsSection appSettings = (AppSettingsSection)applicationConfig.GetSection("appSettings");
appSettings.Settings["uri"].Value;

As shown appsettings value can be read just fine. Is it possible to have configSections in any other config other than the main application's config file?

3 Answers3

0

Are you missing a '.' delimiter?

fileMap.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + "config"; 

add the '.':

fileMap.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config"; 
Luke Willis
  • 8,429
  • 4
  • 46
  • 79
csharptest.net
  • 62,602
  • 11
  • 71
  • 89
0

Configuration settings apply at application (app.config located in the application's root for .EXE, Web root for Web applications) and machine(machine.config located in [System Root]\Microsoft.NET\Framework[CLR Version]\CONFIG) level.

The only other config file used is the policy config file which is used to create assembly versioning policies and is linked to the assembly by making of use of the AL tool. This is obviously what you do not want to do.

Try to merge the add in's config sections into the current application's config section to create one app level config file or else put them in machine.config file.

Lonzo
  • 2,758
  • 4
  • 22
  • 27
0

This does NOT work as you mentioned in the question.

You may be getting feeling that you are able to load a DLL.Config file, but it is not loaded by the application, but it is probably working because you have the same appsettings section in the applications app.config. By Default, every appdomain has a config file and mostly its named after the exe (so the name will be applicationname.exe.config.

By default, this is the file loaded by .net framework to read the configurations from. Hence I would not suggest to maintain a .dll.config file

Now you have two alternatives to achieve what you want to achieve:

Option 1: You can maintain separate config files for every ConfigurationSection

Every class which is inherited from ConfigurationSection has a property called "configSource". In main application.exe.config you can specify a custom section as shown below:

<CustomSection configSource="{relative file name}" />
<appSettings file = "relative file name" />

This way you can keep your configuration sections would be splitted in multiple configuration files and you can still access them using the regular system.configuration syntax.

Refer this for more details.

Option 2: Change the default exe

the default configuration file is with name application.exe.config. It can be changed by using the below syntax

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path);

This way you can set any other xml file as config file for the program.
Please note that you have to call this SetData method before giving first call to Configuration classes (i.e. before system reads the configuration files). You can set your .dll.config as application configuration file and you can read all configuration section from there. Refer this for more details about option 2.

Hope this provides enough information.

Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37