使用类库从web.config中/ app.config中获得配置设置 [英] Getting configuration settings from web.config/app.config using class library

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

问题描述

在3.5配置设置我发疯...帮助! ;)

Configuration settings in 3.5 is driving me nuts... Help! ;)

我有一个类库(命名ADI),需要从项目的一些配置设置使用它(如ConnectionString中,文件系统位置等)。

I have a class library (Named ADI), that needs some configuration settings from the project using it (like connectionstring, filesystem locations etc).

我想在我的Windows窗体/ Web项目的app.config或Web.Config中定义这些设置,像其他的设置。

I want to define these settings in my Windows Forms/Web Projects App.Config or Web.Config, like other settings.

下面是我的app.config我的Windows的一部分窗体应用程序:

Here is part of my app.config for my windows forms application:

<applicationSettings>
    <PhotoImportRobot.My.MySettings>
      <setting name="ADIImageRoot" serializeAs="String">
        <value>C:\DataTemp\ADI\Original\</value>
      </setting>
      <setting name="ADIImageVariantsRoot" serializeAs="String">
        <value>C:\DataTemp\ADI\Variants\</value>
      </setting>
    </PhotoImportRobot.My.MySettings>
</applicationSettings>

我如何访问,从我的类库??

How do I access that from my class library??

我尝试这样做:

System.Configuration.ConfigurationManager.AppSettings("ADIImageVariantsRoot")

怎么办?

推荐答案

如果你不规整后的设置,appSettings部分只需要键值对:

If you're not after structured settings, the appSettings section just takes key-value pairs:

<appSettings>
  <add key="ADIImageRoot" value="C:\DataTemp\ADI\Original\" />
  <add key="ADIImageVariantsRoot" value="C:\DataTemp\ADI\Variants\" />
</appSettings>

这将使你通过的AppSettings字典来访问它们:

This will enable you to access them via the AppSettings dictionary:

ConfigurationManager.AppSettings["ADIImageVariantsRoot"]

正如你所期望的。

As you would expect.

另外,如果你需要更多的结构来配置(即超过仅仅是字符串,或设置的集合),你可以考虑使用自己的配置部分,使用的配置节和它的相关部分。

Alternatively, if you need more structure to your configuration (i.e. more than just strings, or a collection of settings), you can look into using a configuration section of your own, using a ConfigurationSection, and its relevant parts.

这篇关于使用类库从web.config中/ app.config中获得配置设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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