Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
580 views
in Technique[技术] by (71.8m points)

asp.net - Getting configuration settings from web.config/app.config using class library

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

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

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

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

<applicationSettings>
    <PhotoImportRobot.My.MySettings>
      <setting name="ADIImageRoot" serializeAs="String">
        <value>C:DataTempADIOriginal</value>
      </setting>
      <setting name="ADIImageVariantsRoot" serializeAs="String">
        <value>C:DataTempADIVariants</value>
      </setting>
    </PhotoImportRobot.My.MySettings>
</applicationSettings>

How do I access that from my class library??

I tried this:

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

What to do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

<appSettings>
  <add key="ADIImageRoot" value="C:DataTempADIOriginal" />
  <add key="ADIImageVariantsRoot" value="C:DataTempADIVariants" />
</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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...