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

C#.NetWpfConfiguration

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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

Solution 3 - C#

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

Solution 4 - C#

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.

Solution 5 - C#

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.

Solution 6 - C#

This also works

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

Solution 7 - C#

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.

Solution 8 - C#

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

Solution 9 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEdward TanguayView Question on Stackoverflow
Solution 1 - C#Cédric RupView Answer on Stackoverflow
Solution 2 - C#ZigglerView Answer on Stackoverflow
Solution 3 - C#Anand ShahView Answer on Stackoverflow
Solution 4 - C#LarryView Answer on Stackoverflow
Solution 5 - C#user1210085View Answer on Stackoverflow
Solution 6 - C#mehranView Answer on Stackoverflow
Solution 7 - C#Vitaly Asher NovitskyView Answer on Stackoverflow
Solution 8 - C#LaoRView Answer on Stackoverflow
Solution 9 - C#RonaldPaguayView Answer on Stackoverflow