settings file from referenced assembly RRS feed

  • Question

  • Hi everyone,


    I have a query regarding application (& user) settings. I understand the difference between application and user settings, however I am unclear as to what happens to the xml files if I create an application that references say a class library that has application and user settings. In my situation I have a class library that has some data access code. In this class library I have some application settings with the database connection strings. If I build this class library I can see the .config xml file (with the connection strings within) along with the .dll. However, if I create an application which references this data access library and then build the application I can no longer see where the data access get's the connection string from.


    I have a separate database for development and production so I basically want to build the application copy it to 2 locations, open the .config file for the production one and change the database connection string to point to the production database. Is what I am trying to do strange, I don't really want to build the data access code again simply to change the database connection string (as I thought this was the idea of settings), or should my database connection string be in the application that will reference the data access code (although in this case I am unsure how, in the data access code, I can use My.Settings.DBConn if I do this?)





    Tuesday, May 20, 2008 12:50 AM


  • You are missing the <sectionGroup> for the library settings.  Copy it from the library's app.config file.
    Wednesday, May 21, 2008 3:24 AM

All replies

  • Your class library retrieves the settings from the app.exe.config file, not from an assembly.dll.config file. 
    Tuesday, May 20, 2008 3:24 PM
  • Hi nobugz,


    Thanks for you response. I am still not quite clear though, perhaps I did not explain my situation properly (or I am doing something wrong). I create a ClassLibrary1, I add some application settings, I build the ClassLibrary1. I create a WindowsApplication1, I reference ClassLibrary1, I build WindowsApplication1. In the bin/Release directory for WindowsApplication1 there are no .exe.config files (neither WindowsApplication1.exe.config nor ClassLibrary1.exe.config). So I am wondering if I want to change the app settings for ClassLibrary1 (assuming I want to change a DSN name) do I have to re-compile ClassLibrary1 and WindowsApplication1? I assume that when WindowsApplication1 calls a method on a ClassLibrary1 class that uses the application settings, that the application settings are read from somewhere, but where? Are they compiled into ClassLibrary1 (in which case why is there a .exe.config file)?





    Wednesday, May 21, 2008 1:40 AM
  • Look in your library's project folder.  The setting designer created an app.config file there.  You should also see it in the Solution Explorer window.  Merge the contents with any other app.config files you may have to create your appname. exe. config file and copy it into the bin\debug or bin\release folder of your .exe.  It should be appname. exe. vshost. config if you want to test it in the debugger.

    You don't have to recompile anything if you change the value of a setting.  Just edit the .config file.
    Wednesday, May 21, 2008 2:19 AM
  • Hi nobugz thanks for the reply.


    OK. I can see the .exe.config file in the bin/release of the ClassLibrary (and also the app.config file in the ClassLibrary's project folder). I can see the section of the file where the application settings are defined. When you said merge I assume you mean I should copy these settings (so the part of the file <ClassLibrary1.My.MySettings>) and manually paste them into my WindowsApplication1.exe.config file. I did this and tried to run WindowsApplication1 (not in the debugger) but it threw an exception when I tried to call a method on ClassLibrary1.class. The exception states:


    System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section applicationSettings/ClassLibrary1.My.MySettings.


    Perhaps I did not merge the config files properly, but I still cannot quite understand where ClassLibrary1.class is reading its applications settings from when I run WindowsApplication1.





    Wednesday, May 21, 2008 2:58 AM
  • Sound like you didn't merge it properly.  Post your .config file.
    Wednesday, May 21, 2008 3:05 AM
  • Here's the WindowsApplication1.exe.config file. I'll post all contents although I guess it's the applicationSettings sections that is important.


    <?xml version="1.0" encoding="utf-8" ?>
            <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                <section name="WindowsApplication1.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
                <!-- This section defines the logging configuration for My.Application.Log -->
                <source name="DefaultSource" switchName="DefaultSwitch">
                        <add name="FileLog"/>
                        <!-- Uncomment the below section to write to the Application Event Log -->
                        <!--<add name="EventLog"/>-->
                <add name="DefaultSwitch" value="Information" />
                <add name="FileLog"
                     type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
                <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
                <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
                <setting name="test" serializeAs="String">
            <setting name="DefaultAppValue" serializeAs="String">

    Wednesday, May 21, 2008 3:09 AM
  • You are missing the <sectionGroup> for the library settings.  Copy it from the library's app.config file.
    Wednesday, May 21, 2008 3:24 AM
  • Hi nobugz,


    Thanks a lot for that, it worked after I added the entry under <sectionGroup>. A couple of follow up questions, does this have to be manual or is there an automatic way to include application settings from referenced assemblies into the current application? Also, where does ClassLibrary1 read its application settings from if I do not include them in the current applications .exe.config file?





    Wednesday, May 21, 2008 4:03 AM
  • I'm not aware of an automated procedure.  Settings in class libraries are not well supported by the IDE.  Not exactly easy to do either, given that System.Configuration doesn't support library settings.  If you don't include them in the .config file, you'll get the default values.
    Wednesday, May 21, 2008 4:39 AM