views:

5708

answers:

5

I created a App.config file in my WPF application:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appsettings>
    <add key="xmlDataDirectory" value="c:\testdata"/>
  </appsettings>
</configuration>

Then I try to read the value out with this:

string xmlDataDirectory = ConfigurationSettings.AppSettings.Get("xmlDataDirectory");

But it says this is obsolete and that I should use ConfigurationManager which I can't find, even searching in the class view.

Does anyone know how to use config files like this in WPF?

+7  A: 

You have to reference the System.Configuration assembly which is in GAC

Use of ConfigurationManager is not WPF specific : it is the priviledged way to access configuration information for any type of application

Please see Msdn for further info

Hope it helps !

Cédric

Cédric Rup
yes that's it, I had to change the name from appsettings to appSettings, add the System.Configuration reference to my project, and then use this: string xmlDataDirectory = ConfigurationManager.AppSettings.Get("xmlDataDirectory");
Edward Tanguay
A: 

Hi, In you app.config change your appsetting to

<applicationSettings>
    <WpfApplication1.Properties.Settings>
        <setting name="appsetting" serializeAs="String">
            <value>c:\testdata.xml</value>
        </setting>
    </WpfApplication1.Properties.Settings>
</applicationSettings>

Then in code-behind

string xmlDataDirectory = WpfApplication1.Properties.Settings.Default.appsetting.ToString()

HTH

Anand
A: 

This also works

 WpfApplication1.Properties.Settings.Default["appsetting"].ToString()
mehran
A: 

hi, i am using the above code but it appears with an error

object reference not set to an instance of object

A: 

You have to add the reference to System.configuration in your solution. Also include "using System.Configuration;" Once you do that you'll have access to all the configuration settings.

Larry