0

I have a WpfApplication project (Visual Studio 2008) with an app.config generated from entering 'Name' and 'Value' pairs in the Settings.settings file (and therefore generating 'applicationSettings' elements rather than 'appSettings' elements).

I have added a class1 class library project in the same Visual Studio solution.

** I have added the WpfApplication app.config file to the class1 project using "add existing item and then add as link".** since found out this is unneccassary (i.e. string a = ConfigurationManager.AppSettings.Get("key1"); below works without this)

I want to read the values within app.config from class1 and have explored the following:

string s1 = Settings.Default.appsetting1; 

But I do not want to reference the WpfApplication10 project from Class 1 project and so cannot get a reference to the Settings class. So this syntax only works within the WPF project.

string a = ConfigurationManager.AppSettings.Get("key1").ToString(); 

This does work, but only if I add the following to app.config:

<appSettings>
   <add key="key1" value="1"/>
</appSettings>

Are there other ways to achieve what I want which may be better (e.g. offering type safety or being able to read the Settings class properties?

sturdytree
  • 849
  • 3
  • 12
  • 26
  • Ran into this while doing the same research. http://stackoverflow.com/questions/2400097/reading-from-app-config-file – sam yi Feb 08 '12 at 05:55

1 Answers1

5

You can do the following thing: if you need to get value from key="key1" just write this(supposing your app.config is in the correct directory).

string str=System.Configuration.ConfigurationSettings.AppSettings["key1"];
vwvolodya
  • 2,324
  • 2
  • 20
  • 30