I am reading articles, forum posts about applicationSettings for almost a week now.
In almost every thread there was someone that appears to have correctly pointed out that the class libraries cannot have config files when deployed and their applicationSettings configured at design must be specified/merged in the executable.exe.config configuration file of the application that host/consumes the dll.
Not necessarily true.
You can but you don't need to merge them class library settings unless you want to provide the user with a way to "overwrite" the default values - the ones that are specified using the DefaultValueAttribute hard coded in the assembly.
So, for a very simple, practical example. Let's use VB.NET 1. I created a Class Library project called ClassLibrary. 2. Showing all files, expand MyProject, double click Settings.settings. 3. Adding a setting called Message, application scoped whose value is "Hello!". 4. Create a property in Class1.vb (the automatically added class)
Public Class Class1
Public Shared ReadOnly Property Message As String
Get
Return My.Settings.Message
End Get
End Property
End Class
Add a button to the already created Form1 and double click on it. 8 Add the some code to the the Button1_Click handler. Should look like this.
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
MessageBox.Show(ClassLibrary.Class1.Message)
End Sub
End Class
Have WinForm "Set as Startup project"
Now, while in the IDE everything works beautifully. Run the solution and you'll get the expected Hello! when you press the button. If you go and change the setting in the app.config of the library to say "Good bye!" and you run the solution again you get a "Good bye!"
However, right click on the WinForm project and "Open in Explorer" and get to the Debug folder. There's no WinForm.exe.config file yet. Let's create one quickly. Switch back to VS and while the WinForm project is selected click to Show All Files. Expand MyProject, open Settings.settings, create a setting (doens't matter what) and save. There we go, an App.config was created and if I build this solution, the Debug folder will contain a WinForm.exe.config.
Tell me how I merge my Message setting from the class library config
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ClassLibrary.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<ClassLibrary.My.MySettings>
<setting name="Message" serializeAs="String">
<value>Hello!</value>
</setting>
</ClassLibrary.My.MySettings>
</applicationSettings>
</configuration>
into the WinForm's config
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="WinForm.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>
<applicationSettings>
<WinForm.My.MySettings>
<!--<setting name="A" serializeAs="String">
<value>A</value>
</setting>-->
</WinForm.My.MySettings>
</applicationSettings>
</configuration>
so I can change the Message setting's value in the WinForm.exe.config to something else and the application will display this new value overriding the DefaultValueAttribute.
I have done some research lately for this applicationSettings problem. I found two relatively convenient ways of doing what I was asking for. I put my thoughts about it together and wrote a blog entry here.
2
3
For deployment you will have to create a configuration section in the main config file and point to
your modified "ClassLibrary.dll.config":
You don't have to point to external files though, the config sections declarations can point to their definitions further down in the main config file like this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With