Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 650
Edward Tanguay Avatar asked Apr 30 '09 09:04

Edward Tanguay


People also ask

How read app config file in C# Windows application?

Add the App. config file to your project. After creating a . NET Framework project, right-click on your project in Solution Explorer and choose Add > New Item. Choose the Application Configuration File item and then select Add.

What is the use of app config in C#?

App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.


2 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.

like image 62
Cédric Rup Avatar answered Sep 27 '22 22:09

Cédric Rup


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(); 
like image 45
Ziggler Avatar answered Sep 27 '22 21:09

Ziggler