.Net WPF – Using an Application Configuration File

4 Comments

This is going to be a real short post about an issue that I ran into recently with WPF.

I was wanting to use an app config file with my WPF application.  I was running into an issue with the ConfigurationManager calls not getting the information from the app config file.

I added the app config just like I would in a Windows Form..
AppConfig

My app config file looked something like this…

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
     <add key="myKey" value="SomeValue" />
  </appSettings>
</configuration>

I was using the standard code that works fine in Windows Forms(needed to add a reference to the System.Configuration):

using System.Configuration

private void Window_Loaded(object sender, RoutedEventArgs e)
{
     var myKey = ConfigurationManager.AppSettings["myKey"];
}

However, the app settings were not being found.  After scratching my head for a while and searching numerous places online, I found a small response on a forum.  None of the “MVP”s picked up on it.  It was because of the name of the app config file.  By default, VS2008 added the file as “App1.config”.  For some reason, the ConfigurationManager class looks for a file named exactly “App.config”.

So simply changing the name of the config file to “App.config” fixed the issue and the code started working.

Tags: , , ,

4 Responses to “WPF – Using an Application Configuration File”

  1. Samuel Gallardo Says:

    Aw, this was a really nice post. In concept I wish to put in writing like this additionally ñ taking time and actual effort to make a very good articleÖ but what can I sayÖ I procrastinate alot and under no circumstances seem to get something done.

  2. Margarette Bohney Says:

    Hi, i think that i saw you visited my weblog so i came to ¡§return the favor¡¨.I am attempting to find things to improve my site!I suppose its ok to use some of your ideas!!

  3. Vigneshwaran Says:

    you’r a genius. Keep it up……:):):)

  4. Marquis Niesborella Says:

    Did you go through a formal legal separation that specified what bills were who’s responsiblity? If you don’t have a legal document stating who’s responsible for what, you might be screwed. Before the divorce can be final all these bills will have to be worked through and decided who is paying what. Yep, it’s going to go against your credit (if it’s a joint card) and spousy doesn’t pay it. If you shared 50/50 custody of your kids then half their debt was already paid for by you by having the kids 50% of the time.

Leave a Reply