14

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?

Kjensen
  • 12,447
  • 36
  • 109
  • 171

4 Answers4

23

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.

Jack Scott
  • 442
  • 4
  • 10
Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
  • 1
    I decided to switch to using the old appsettings - and that works. I also wrote a wrapper to check if it exists and throw an error if it does not. – Kjensen Mar 28 '09 at 10:47
  • Yep - throwing an ArgumentNullException is probably the way to go if it's not supplied. – Zhaph - Ben Duguid Mar 28 '09 at 20:30
  • @alperebicoglu I don't believe they are duplicates: this one knows about the difference and wants to know how to move and read the settings from the appropriate config files, while the other wants to know what the difference between what was forms (app.config) or ASP.NET (web config) configuration files are. – Zhaph - Ben Duguid Sep 10 '18 at 08:45
2

Add reference System.web; add name space and user

using System.Web.Configuration;    

String webConfigValue;
            webConfigValue = WebConfigurationManager.AppSettings["employeeDB"].ToString();

to read the web config value

<appSettings>
        <add key="employeeDB" value="Data Source=servername;Initial Catalog=employee;Persist Security Info=True;User ID=userid;Password=password;"/>        
</appSettings>
Jon B
  • 51,025
  • 31
  • 133
  • 161
2

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)

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
  • 1
    OP (and I) want to know how to read the setting from the class library. – Tuan Aug 10 '11 at 05:57
  • @Code Blue: Thanks for commenting. In that case, move the .settings (and the generated code will follow) into said class library, but be sure to replicate the settings (if any) that puts into the app.config of the library into your exe's app.config. (There's no nice way to have it auto-sync the default values out to outer projects) – Ruben Bartelink Aug 10 '11 at 10:27
0

For ApplicationSettings you should use:

[YourNamespace].Properties.Settings.Default.[YourSettingName]

This provides a strongly typed reference to your setting and returns the default value if one is not defined in the web.config file. For AppSettings you should use:

System.Web.Configuration.WebConfigurationManager.AppSettings
xr280xr
  • 12,621
  • 7
  • 81
  • 125