How to use a App.config file in WPF applications?
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 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();
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()