rschuler rschuler - 9 months ago 44
C# Question

Reading dll.config (not app.config!) from a plugin module

I am writing a C# .NET 2.0 .dll that is a plug in to a Larger application. The visual studio project for my module has a app.config file which is copied to a MyProj.dll.config along side of MyProj.dll.

The plan is that MyProj.dll.config will be edited after the .dll is deployed. I am trying to read my settings from that modified local file. I have tried pulling out the LocalFilesSettingsObject and changing it's application name to my .dll like this:

Properties.Settings config = Properties.Settings.Default;
SettingsContext context = config.Context;
SettingsPropertyCollection properties = config.Properties;
SettingsProviderCollection providers = config.Providers;
SettingsProvider configFile = Properties.Settings.Default.Providers["LocalFileSettingsProvider"];
configFile.ApplicationName = Assembly.GetExecutingAssembly().GetName().Name;
config.Initialize(context, properties, providers);

That is not working. I am struggling to wrap my head around the whole .NET Settings mess. I'd like a recipe to finish this task. I would also like a link to a clear explanation (with examples) of how settings are supposed to work in .NET 2.0

Answer Source

You will need to load the x.dll.config (with the Configuration API) yourself. All the automatic file handling (including the .Settings) is all about machine.config/y.exe.config/user-settings.

To open a named config file:

  • Reference System.Configuration.dll assembly.
  • Using System.Configuration
  • Create code like:

    Configuration GetDllConfiguration(Assembly targetAsm) {
      var configFile = targetAsm.Location + ".config";
      var map = new ExeConfigurationFileMap {
        ExeConfigFilename = configFile
      return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);