Home >>

Reading From App.Config File (C#)


Reading From App.Config File (C#)

Recently I was on site at a customer engagement and ran into a little trouble when asked to do some C# development. C# is not what I would consider one of my strengths as I simply have not had to use it much in my career. However, as a consultant it is my job to adapt and get the job done. Needless to say I began to really enjoy development in C#. However, I did stumble across a problem that took me a couple of hours to solve when it should have taken a few minutes. Read below the problem I ran into and the few quick steps that I uncovered that are necessary to solve this easy to implement functionality.

The Background

On a customer engagement I was tasked with writing an database driven application that performed a variety of operations (read and write) against the database. The proof of concept was coded with database connection information hard coded as constants on top of an already existing code base.

The Issue

In order to move the proof of concept application further I was asked to move the database connection information from the hard coded constants to the App.config file for the C# application. Being somewhat of a novice to C# not knowing a few minor details cost me more time than it should have.

The Resolution

In resolving this issue there were two main problems I uncovered.

  1. In order to use the ConfigurationManager class to load the App.config file, a reference to the System.Configuration assembly had to be added as a reference to the project.
  2. The App.config file generates a class file based on the name of the project (very specifically) that is loaded at run time. The project my code was in was started as a “Class Library” project so the generated config file was not obvious.
error message

Identifying and solving the first problem was fairly easy. When trying to build the project I was getting the following error: The name ‘ConfigurationManager’ does not exist in the current context

A simple google search should provide you with enough information to identify this issue and resolve it. I’ve seen some posts that insist that you have to add multiple references including direct reference to the .dll. This has not been the case for me. I encountered the issue in Microsoft 2008 on site, and reproduced it in Microsoft 2005 at home resolving in both environments with simply adding the .NET reference through Visual Studio (see below).

The second issue was a bit more difficult to uncover. Unfortunately, the project was given to me to work with as a “Class Library” and not a typical “Windows Application” or “Console Application“.

This small difference meant about 2 hours of frustration that didn’t need to exist. In order to generate the .config file the project must be an executable application. Visual Studio will take the App.config file and regenerate it as something the executable can read at build time. Let’s say the executable of your project is named MyProject.exe, the App.config file associated with the project will be regenerated at build time with the name MyProject.exe.config. Because the project was a “Class Library” project, there is no executable associated with it. So, although I had a perfectly well configured App.config file, none of the values were being read. Eventually I stumbled on some code that would allow me to add configs on the fly. This generated a file that looked like ClassLibrary.csproj.vshost.config which actually contained the config that was added on the fly. I then noticed that if I added the database configurations I was trying to load to that file that they were being pulled in successfully.

If you find yourself trying to load configs from an App.config file and no values are coming through, double check the type of Visual Studio project you are working with. If it is a Class Library type of project then this is your problem. The configs should be loaded by a type of project that has an executable.

Sounds like a pretty rookie mistake because it is a rookie mistake. Being unfamiliar with C# development I didn’t think to check the project type and tried to add some proof of concept code to a project that was not of the right type. Although the problem was in the end a very simple problem to solve, it caused me enough pain that I decided to IssueBlog It!

Published by John Zeren

John Zeren is a software engineering professional with a concentrated background in, and passion for, web application development. As a technical and a people leader in the tech space, he is a champion of agile methodologies, collaboration, and using iterative development to solve complex problems.

Leave a comment

Your email address will not be published. Required fields are marked *