Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared configuration between an .exe and a .dll

I'm trying to work with a settings.settings file in my project. There are values that need to be shared between the .exe file and various DLLs. I'd rather not just pass these values around, I'd like to access them when I need them but each project sets up its values with slightly different names and therefore aren't reachable by the other projects.

Is there any way to share the contents of the app.config file between an .exe and a .dll using the settings.settings approach? Or do I need to go back to using ConfigurationManager in order to do this?

like image 738
Jeff Hornby Avatar asked Sep 07 '11 15:09

Jeff Hornby


1 Answers

Just put your settings in the App.config file, and read them from your dll. In fact I believe it's the only place your dll will look for settings/config, local config for the dll is ignored.

Here's a quick example to ensure the dll has no strong references to the application. This code isn't great but you get the idea.

  private string GetSettingValue(string key)
  {
     string executingAssembly = Assembly.GetEntryAssembly().GetName().Name;
     string sectionName = "applicationSettings/" + executingAssembly 
                                                 + ".Properties.Settings";
     ClientSettingsSection section =
            (ClientSettingsSection)ConfigurationManager.GetSection(sectionName);

     // add null checking etc
     SettingElement setting = section.Settings.Get(key); 
     return setting.Value.ValueXml.InnerText;
  }

Alternatively have a common dll with the shared settings and take a dependency from each assembly that needs to share the config. This is far cleaner.

like image 144
TheCodeKing Avatar answered Oct 16 '22 10:10

TheCodeKing