How to combine class library applicationSettings into a host configuration executable

I have been reading articles, forum posts about applicationSettings for almost a week.

In almost every thread, there was someone who seemed to correctly point out that class libraries cannot have configuration files during deployment, and their application settings configured in the project should be specified / combined in the executable.exe.config configuration file of the application this host / uses the dll.

Not necessarily true.

You can, but you do not need to combine their class library settings if you do not want to provide the user with a way to "overwrite" the default values ​​- those specified using 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. With all the files displayed, expand MyProject, double-click Settings.settings. 3. Adding the Message parameter, an application area whose value is Hello! 4. Create a property in Class1.vb (automatically added class)

Public Class Class1 Public Shared ReadOnly Property Message As String Get Return My.Settings.Message End Get End Property End Class 
  • Create a VB WinForms project and name it WinForm.
  • Add a link to the ClassLibrary project.
  • Add a button to the already created Form1 and double-click on it. 8 Add the code to 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 

    Final class

  • WinForm has "Install as Startup Project"

Now that everything works beautifully in the IDE. Run the solution and you will get the expected Hello! when you press the button. If you go over and change the setting in the app.config library to say "Goodbye!". and you run the solution again, you get "Goodbye!"

However, right-click on the WinForm project and "Open in Explorer" and go to the Debug folder. There is no WinForm.exe.config file yet. Let me quickly create it. Go back to VS, and when the WinForm project is selected, click Show All Files. Expand MyProject, open Settings.settings, create a setting (no matter what) and save. There we go, App.config was created, and if I create this solution, the Debug folder will contain the WinForm.exe.config file.

Tell me how I combined my Message parameter from the class library configuration

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

to WinForm configuration

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

therefore, I can change the value of the Message parameter in the WinForm.exe.config file to something else, and the application will display this new value by overriding the DefaultValueAttribute attribute.

+6
source share
1 answer

Recently, I have done some research for this ApplicationSettings problem. I found two relatively convenient ways to do what I requested. I talked about this together and wrote a blog post here .

+3
source

Source: https://habr.com/ru/post/912990/


All Articles