Jürgen Steinblock
Jürgen Steinblock

Reputation: 31733

simple way to read settings of an app.exe.config file from a linked dll

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:

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.

Upvotes: 1

Views: 4135

Answers (2)

Albin Sunnanbo
Albin Sunnanbo

Reputation: 47048

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;

Upvotes: 1

Mark H
Mark H

Reputation: 13897

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).

Upvotes: 4

Related Questions