Wednesday, 28 March 2018

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:






C:\DataTemp\ADI\Original\


C:\DataTemp\ADI\Variants\






How do I access that from my class library??



I tried this:



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



What to do?


Answer



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










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.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...