Stécy
Stécy

Reputation: 12339

Using multiple .settings classes from a class library, how to proceed?

Having two projects: one is a class library (a .dll assembly) and the other is a GUI (.exe) using the class library.

In the class library, I've added two .settings files (which automatically adds the app.config file to the project) using all "application" settings. When I compile the project I effectively get the MyLib.dll.config file in the output directory.

Now, for the GUI project, I reference the class library and manually copy the MyLib.dll.config to the bin\debug (and release) folder.

I am trying to get the MyLib.dll.config settings from the GUI application but so far it is not working. I've tried having the .settings classes set to public but I was unsuccessful.

I've read about the OpenMappedExeConfiguration() method but I can't seem to find the settings in the returned config. Furthermore, how would I force the .settings classes to use the config returned by OpenMappedExeConfiguration?

Note: I do not want to manually add the entries from MyLib.dll.config to the app.config of the GUI application since those entries do not belong there at all.


Additional notes:

The .config file that I have is:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="TestAssembly.ItemClass" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
            <section name="TestAssembly.Entity" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <TestAssembly.ItemClass>
            <setting name="Job" serializeAs="String">
                <value>TrussPlant.Job</value>
            </setting>
            <setting name="Family" serializeAs="String">
                <value>TrussPlant.Family</value>
            </setting>
        </TestAssembly.ItemClass>
        <TestAssembly.Entity>
            <setting name="TrussClassifier" serializeAs="String">
                <value>TrussPlant.Classifier</value>
            </setting>
            <setting name="TrussComposer" serializeAs="String">
                <value>TrussPlant.Composer</value>
            </setting>
        </TestAssembly.Entity>
    </applicationSettings>
</configuration>

Note that there is a SectionGroup which contains the two sections.

Upvotes: 0

Views: 521

Answers (2)

csharptest.net
csharptest.net

Reputation: 64218

Assuming your DLL project has something like this in .\Properties\Settings.Designer.cs:

namespace _SampleApplication.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
...

Then you can add this to another file in your DLL project with the following code:

namespace _SampleApplication.Properties {
    partial class Settings
    {
        static System.Configuration.Configuration _config = null;

        private System.Configuration.Configuration DllConfig
        {
            get
            {
                return _config != null ? _config : 
                    _config = System.Configuration.ConfigurationManager.OpenExeConfiguration(this.GetType().Assembly.Location);
            }
        }   

        public override object this[string propertyName]
        {
            get { return DllConfig.AppSettings.Settings[propertyName].Value; }
            set { DllConfig.AppSettings.Settings[propertyName].Value = value as string; }
        }
    }
}

More of a 'hack' solution, I don't know how to do right, but it will work.

Upvotes: 1

Michael Maddox
Michael Maddox

Reputation: 12489

In general, config settings should belong to the application, not to the component.

That said, the component should be able to read its own settings, so add a method to the component that reads the settings and then call that method from the application.

Upvotes: 0

Related Questions