Can a class library have an App.config file?

后端 未结 7 987
别跟我提以往
别跟我提以往 2020-11-27 04:04

Here is what my solution is looking like at the moment:

\"enter

In the Tutomen

相关标签:
7条回答
  • 2020-11-27 04:44

    Just create your own XML file, name it appConfig.xml or something similar, have your class library read the file using System.Xml instead of System.Configuration, and package the file along with your dll.

    0 讨论(0)
  • 2020-11-27 04:50

    You can add Settings File which will automatically generate app.config file.

    And can Add the key value pairs in tabular form with the datatype specified. Then Access the values by calling Settings.Default.[KEY]

    can reffer: https://www.technical-recipes.com/2018/creating-a-user-settings-class-library-for-your-c-project/

    0 讨论(0)
  • 2020-11-27 04:51

    No, class libraries can hold setting files, but their values will be defined in the application configuration (web.config, app.config...).

    That's because of configuration settings overriding feature.

    You'll need to declare the assemblies' configuration sections in the app.config or web.config of your application (WPF, SL, ASP.NET...) and define a value for a particular number of settings defined in the proper assembly settings.

    EDIT: Add a setting file to your project and add a setting with application scope, and your assembly would have something like this:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                <section name="Assembly1.Settings1" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
            </sectionGroup>
        </configSections>
        <applicationSettings>
            <Assembly1.Settings1>
                <setting name="settingA" serializeAs="String">
                    <value>a value</value>
                </setting>
            </Assembly1.Settings1>
        </applicationSettings>
    </configuration> 
    

    Now you'd need to go to your application, and you need to copy-paste the section group, and section declarations, and the definition of the values for the settings. That's all.

    0 讨论(0)
  • 2020-11-27 04:52

    Any specific configuration from library app.config, you have to put in your exe configuration file manually.

    0 讨论(0)
  • 2020-11-27 04:56

    Well the class library can't have its own app.config, but if you define your settings the from the class library in the app.config for the exe file the class library should be able to find those to..

    im a bit unsure but i don't think it will combine them automatically i guess you have to do i manually!

    0 讨论(0)
  • 2020-11-27 04:58

    While this is an older thread, It does warrent another look.

    It seems you may want to look at the issue in a different way.

    Class libraries by nature are supposed to be portable. So, any configuration needed should be passed to the class, instead of residing with the library. Things like connection strings are by nature transitory, so it makes sense to put them in the owning application.

    When utilizing the methods contained in the library, you pass any needed information as part of the method's signature, or as a public property in the class. I suggest you create public properties for your configuration items, and pass them when you instantiate the class.

    Now you have no issues with an app.config for the DLL, and the DLL is then truly portable.

    0 讨论(0)
提交回复
热议问题