views:

79

answers:

2

Is there a simple way to read from the global application.exe.config file from a dll? Currently I am loading the file as an XmlDocument but I wonder if there is a better solution.

That's what I mean:

  • If I create a new WinForms Project I have a Settings Tab in the Project properties where I can add some simple values (And I want to access the per Application settings, not the user beased ones).
  • From my code I can access these values with:

    Console.WriteLine(Properties.Settings.Default.SomeValue);
    

The Settings class is autogenerated in the file Settings.Designer.cs.

Now I have the case where a dll need's to read the settings from the Main Application's config file. Is there a simple way to achive this? Currently I am reading the file as an XML Document.

+2  A: 

If you implement an ApplicationSettingsBase in your DLL, it will actually read the application.exe.config anyway - you just need to add the relevant config sections to it. The .dll.config file output by the compiler is generally a useless piece of crap, because the DLL won't even look at it.

The easiest way to get what you want is to add yourself a settings file to the DLL and use it within the DLL - when the app.config is generated, copy those sections over to the app.config file in the executable. (Unfortunately this isn't automated, it should be).

Mark H
+1  A: 

It depends on where the properties belong.

If you create a Settings file in your dll you can just copy the config section to app.config of the main file and your dll will read its settings as usual.

If the settings are "global", you can change the "Access modifier" in the settings designer from Internal to Public. But you need to reference the main project, but that usually creates circular references. The way to solve that is to create a class library with the global settings (change modifier to public) that you can reference from all of your projects.

var fooSettingValue = SettingsProject.Properties.Settings.Default.FooSetting;
Albin Sunnanbo
Jepp, copying the config section from the dll's app.config file to the main app.config did the trick. I also needed to add an entry to the `<sectionGroup name="applicationSettings" ...` entry. Now my dll reads it's settings from the main .config file. thx.
SchlaWiener