将 app.config 与类库一起使用 [英] Using app.config with a class library

查看:21
本文介绍了将 app.config 与类库一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我曾考虑通过类库中的代码手动读取配置设置,但这也是一个主要的痛苦.有没有人对在类库中加载 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?

推荐答案

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

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.

请参阅已接受的答案"https://stackoverflow.com/questions/396139/create-your-own-settings-in-xml">这个问题 了解这方面的信息.

See the accepted answer in this question for information on this.

这篇关于将 app.config 与类库一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆