Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:\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??

I tried this:

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

What to do?

like image 355
Kjensen Avatar asked Mar 26 '09 10:03

Kjensen


2 Answers

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>

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.

like image 92
Zhaph - Ben Duguid Avatar answered Sep 21 '22 01:09

Zhaph - Ben Duguid


You seem to be using the Settings stuff built into visual studio. This generates a wrapper class related to the file, called, in your case MySettings.

You can thus write something like MySettings.Instance.ADIImageVariantsRoot. (If you click show all files in the project toolbox, it will show you the .settings.cs file and you can see all the gory details)

like image 34
Ruben Bartelink Avatar answered Sep 21 '22 01:09

Ruben Bartelink