How to use a App.config file in WPF applications?

Viewed 172127

I created an 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?

9 Answers

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

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

Please see Microsoft Docs - ConfigurationManager Class for further info.

In your 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 the code-behind:

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

There is a good article about Application settings on Microsoft. According to that you need to:

  • manually create App.config file (from Project Context menu -> Add -> New Item... -> Application Configuration File.) Dialog window for adding application config file
  • add required sections there (I'm using only application scope settings):
<?xml version="1.0" encoding="utf-8"?>

<configuration>
    <configSections>
        <sectionGroup name="applicationSettings"
                      type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="DevelopmentEnvironmentManager.WPF.Properties.Settings"
                     type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <DevelopmentEnvironmentManager.WPF.Properties.Settings>
            <setting name="SqliteDbFilePath" serializeAs="String">
                <value>Database.db</value>
            </setting>
            <setting name="BackgroundColor" serializeAs="String">
                <value>White</value>
            </setting>
            <setting name="TextColor" serializeAs="String">
                <value>Black</value>
            </setting>
        </DevelopmentEnvironmentManager.WPF.Properties.Settings>
    </applicationSettings>
</configuration>

NOTE: Replace 'DevelopmentEnvironmentManager.WPF' with the name of your application.

Additionally, you can go to Properies of the project and add Settings.Designer: Project settings this will add convenient designer to your project, so you don't have to edit XML manually: Location of the file in Solution exporer Settings designer in action

To access settings from the code - simply save and close all config editors, build app and access static Propeties (again, do not forget to change app name in the namespace):

string databasePath = DevelopmentEnvironmentManager.WPF.Properties.Settings.Default.SqliteDbFilePath;

Accessing settings from code

Related