.net 将 app.config 与类库一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/690313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:24:54  来源:igfitidea点击:

Using app.config with a class library

.netconfiguration

提问by Billkamm

Frequently I need to create a .Net class library that requires an app.config for things such as database connection strings. However, these settings must be in the calling application's app.config or web.config. If I want to distribute the DLL across multiple applications it becomes a pain because I have to copy these settings into each the application's app.config.

我经常需要创建一个 .Net 类库,它需要一个 app.config 来处理诸如数据库连接字符串之类的事情。但是,这些设置必须在调用应用程序的 app.config 或 web.config 中。如果我想在多个应用程序之间分发 DLL 会变得很痛苦,因为我必须将这些设置复制到每个应用程序的 app.config 中。

I have considered manually reading the config settings via code from inside the class library, but that is also a major pain. Does anyone have any suggestions for the best way to load app.config settings inside a class library?

我已经考虑通过类库中的代码手动读取配置设置,但这也是一个主要的痛苦。有没有人对在类库中加载 app.config 设置的最佳方式有任何建议?

采纳答案by AaronS

One thing you could do is to have a seperate settings file just for your library, then you only have to have a reference to it in your entry apps config file.

您可以做的一件事是为您的库创建一个单独的设置文件,然后您只需在入口应用程序配置文件中引用它。

See the accepted answerin this questionfor information on this.

接受的答案这个问题有关信息。

回答by casperOne

I know it's not what you want to hear, but the entry point's config file is the logical place to put these setttings. If every library required its own config file then it would get tedious to have to edit every single file instead of just one.

我知道这不是您想听到的,但入口点的配置文件是放置这些设置的合乎逻辑的位置。如果每个库都需要自己的配置文件,那么必须编辑每个文件而不是一个文件会变得很乏味。

I would have a text file with the default settings included as part of your library (and as part of the project) which can be distributed with the library for the default settings configuration (it can be copied and pasted into the config file).

我将有一个文本文件,其中包含作为库的一部分(以及项目的一部分)的默认设置,它可以与库一起分发以进行默认设置配置(可以将其复制并粘贴到配置文件中)。

回答by Ian Nelson

Have each class library define configuration settings in a custom ConfigurationSection.

让每个类库在自定义 ConfigurationSection 中定义配置设置。

Then add custom section handlers to your process.exe.config file.

然后将自定义部分处理程序添加到 process.exe.config 文件。

This MSDN articleis pretty comprehensive in its explanation, with examples in both VB and C#.

这篇 MSDN 文章的解释非常全面,在 VB 和 C# 中都有示例。

It involves writing some pretty repetitive code - Dmitryr has created this toolto generate the config sections from a fragment of a XML configuration, which might help.

它涉及编写一些非常重复的代码 - Dmitryr 创建了这个工具来从 XML 配置的片段生成配置部分,这可能会有所帮助。

回答by Joseph Korinek

Here is an alternative.

这是一个替代方案。

Instead of attempting to find ways to "merge" your app.configs, or even placing configuration items in your DLL (and trying to work around what is "by design") in the first place, try this:

与其尝试找到“合并”您的 app.configs 的方法,或者甚至将配置项放置在您的 DLL 中(并尝试解决“按设计”的问题),不如试试这个:

Create your DLL, but where you need a Config Item, create a public Property. When you instantiate the class in the DLL, you populate these properties with config items from the owning application.

创建您的 DLL,但在您需要配置项的地方,创建一个公共属性。当您在 DLL 中实例化类时,您使用来自拥有应用程序的配置项填充这些属性。

Now the configuration is managed at the owner level, and the DLL is portable, as you have to pass what you need to it at run time.

现在配置在所有者级别进行管理,并且 DLL 是可移植的,因为您必须在运行时将需要的内容传递给它。

No more DLL.config!

没有更多的 DLL.config!

回答by Parthiban

Go to App properties -> Settings tab; add your settings there.

转到应用程序属性 -> 设置选项卡;在那里添加您的设置。

Use Properties.Settings.Default.<SettingName>to access the setting. That's it!

使用Properties.Settings.Default.<SettingName>访问设置。就是这样!

回答by Parthiban

Create your class library in a different solution. If you keep them in the same solution, the app.config at the highest hierarchy will get read instead.

在不同的解决方案中创建您的类库。如果您将它们保留在同一个解决方案中,则将读取最高层次结构的 app.config。

回答by MPO

You could create your own XML file(call it appConfig.xml or something if you want), use System.Xml instead of System.Configuration to read it, and include it alongside your dll.

您可以创建自己的 XML 文件(如果您愿意,可以将其称为 appConfig.xml 或其他名称),使用 System.Xml 而不是 System.Configuration 来读取它,并将其包含在您的 dll 中。

回答by habakuk

It is no (big) problem to use its own config file for every class library. If you use vb.net you can add values via the "my project" panel on the "Settings" page.

为每个类库使用自己的配置文件没有(大)问题。如果您使用 vb.net,您可以通过“设置”页面上的“我的项目”面板添加值。

The only thing you have to do is to call "My.Settings.Save" on your class library settings (and not only on your main application settings file).

您唯一需要做的就是在您的类库设置上调用“My.Settings.Save”(而不仅仅是在您的主应用程序设置文件上)。

That works with c#, too (But probably needs more manual work).

这也适用于 c#(但可能需要更多的手动工作)。

回答by MacGyver

If you're using NUnit, name your app.config file with the same name as your *.nunit project filename. So for example, if you called your project "ClassLibraryA.nunit", then name your class library configuration file "ClassLibraryA.config". They also need to reside in the same folder/directory. NUnit is actually using this as the main configuration file.

如果您使用NUnit,请使用与 *.nunit 项目文件名相同的名称命名您的 app.config 文件。例如,如果您将项目命名为“ClassLibraryA.nunit”,则将类库配置文件命名为“ClassLibraryA.config”。它们还需要驻留在同一文件夹/目录中。NUnit 实际上就是使用这个作为主要的配置文件。

If you have custom code, then you need to make sure your app.config (or web.config) is referencing the class library's app.config file. The application only cares about the root app.config or web.config. So you need to add a custom configuration section that points to the class library app.config.

如果您有自定义代码,那么您需要确保您的 app.config(或 web.config)正在引用类库的 app.config 文件。应用程序只关心根 app.config 或 web.config。因此,您需要添加一个指向类库 app.config 的自定义配置部分。