Load config file dynamically

There are time you would like to load your configuration data from a specific config file or maybe even from the config file but not from the main one, I’ll explain it using an example from the real world (last week refactoring).
We have many client which run the ArcGis Explorer (AGX) client (Geographic application like Google Earth), this client can be installed on each and every client, and it has its own configuration files. Within our team we write tasks that running on that AGX. while opening the application it will download to your profile directory the relevant DLL’s for running those tasks. but hey those DLL’s will be in a separate directory, neither in the GAC nor in the AGX executable directory. You can find many examples for such scenarios.


Here you can see the two different directories: one for the E2.exe and its config file, and the specific task DLL’s and its config file (Sample.dll.config)

All we wanted to make is a Task specific configuration. so we would like it to work on every other task we will create.
First you might know this bunch of code, this will help you load the Sample.dll.config file from the wanted directory (The Task directory).

   1:  // Get the application configuration file path.
   2:  string exeFilePath = @"C:\ShaniData\Projects2008\ConfigHandlerDemo\Sample.dll.config"; 
   3:   
   4:  // Map to the application configuration file.
   5:  ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
   6:  configFile.ExeConfigFilename = exeFilePath;
   7:  System.Configuration.Configuration config =
   8:  ConfigurationManager.OpenMappedExeConfiguration(configFile,
   9:  ConfigurationUserLevel.None);
  10:              
  11:  ConfigurationSection sec = config.GetSection("MailManagerConfiguration");

In this situation we should use the .Net default ConfigurationSection implementations (for example: the ApplicationSettingsSection). Those of you who think of using the DictionarySectionHandler or NameValueSectionHandler will found it returning the DefaultSection instead.

Pay also attention that your code is running from the AGX installation directory so if you’ll write your own section it won’t work unless it was installed on the given computer (in GAC or in the AGX installation directory).

 

Shani Raba

Juggling between Performance Optimization, Agile Training, Continuous Delivery, Get Things Done. Role: Group Leader. Moto: Love your coders.