Edward Tanguay
Edward Tanguay

Reputation: 193472

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

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?

Upvotes: 84

Views: 196165

Answers (9)

LaoR
LaoR

Reputation: 1317

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

Upvotes: 6

Anand Shah
Anand Shah

Reputation: 14913

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()

Upvotes: 19

Ziggler
Ziggler

Reputation: 3500

In my case, I followed the steps below.

App.config

<configuration>  
   <startup> 
       <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>

 <appSettings>
   <add key="POCPublishSubscribeQueueName" value="FormatName:Direct=OS:localhost\Private$\POCPublishSubscribe"/>
 </appSettings>

</configuration>

Added System.Configuartion to my project.

Added using System.Configuration statement in file at top.

Then used this statement:

string queuePath = ConfigurationManager.AppSettings["POCPublishSubscribeQueueName"].ToString();

Upvotes: 35

C&#233;dric Rup
C&#233;dric Rup

Reputation: 15958

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.

Upvotes: 86

Vitaly Asher Novitsky
Vitaly Asher Novitsky

Reputation: 59

You can change configuration file schema back to DotNetConfig.xsd via properties of the app.config file. To find destination of needed schema, you can search it by name or create a WinForms application, add to project the configuration file and in it's properties, you'll find full path to file.

Upvotes: 2

Larry
Larry

Reputation: 103

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.

Upvotes: 6

RonaldPaguay
RonaldPaguay

Reputation: 335

I have a Class Library WPF Project, and I Use:

'Read Settings
Dim value as string = My.Settings.my_key
value = "new value"

'Write Settings
My.Settings.my_key = value
My.Settings.Save()

Upvotes: -2

user1210085
user1210085

Reputation: 71

You have to reference System.Configuration via explorer (not only append using System.Configuration). Then you can write:

string xmlDataDirectory = 
    System.Configuration.ConfigurationManager.AppSettings.Get("xmlDataDirectory");

Tested with VS2010 (thanks to www.developpez.net). Hope this helps.

Upvotes: 7

mehran
mehran

Reputation: 21

This also works

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

Upvotes: 2

Related Questions