Using app.config file for a console application in C#

Thursday, June 30, 2011
By keiji
Category: C#, How to
Using app.config for a console application in C# is very convenient, but setting up for that is actually a little bit inconvenient.
So, here is the how to:

  1. Add "System.Configuration" reference to the project's "References"
    To do so, right click on "References" folder of the project, and choose "Add Reference" menu item, choose ".NET" tab, and then choose "System.Configuration" component.
  2. In a code, use "using System.Configuration;" statement.
  3. Add a "App.config" item
    ("add new item..." - "C# Item" - "Application Configuration File")
    This will add an "app.config" file to the project.

The app.config file use following structure:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name=""
            connectionString=""
            providerName="System.Data.SqlClient" />
    </connectionStrings>

  <appSettings>

    <add key="flag1" value="abcdefg" />
    <add key="LogPath" value="log" />

  </appSettings>

</configuration>

The "connectionStrings" section will be added if you use ADO.NET Entity Data Mode.
"<appSettings>" section is the section you need to add. Key/Value pair are defined like this way.

A code to read the configuration values are as follows:

string connectionString = String.Format("{0}",
    ConfigurationManager.ConnectionStrings["RMLS_ORConnectionString"]
);
System.Console.Write(
    String.Format("Connection String={0}\n", connectionString)
);

string flag1= String.Format("{0}",
    ConfigurationManager.AppSettings["flag1"]
);
System.Console.Write(String.Format("flag1={0}\n",flag1));

Using String.Format is to avoid null exception.

When deploying, there is one thing to note.
The app.config is a file name used in a project. However, when it is compiled, the file name becomes application's exe name + .config.
For example, if the application name is "sample.exe", then the configuration name should be "sample.exe.config".

(When first time I used this feature, I copied app.config to the exe folder, and it didn't work... It took a while to figure out why :(

Comments

Re: Using app.config file for a console application in C#
#1Manoj Bhatt @ 02/28/12 4:03 AM
Very informative post. It is very simple and understandable due to which it's more helpful for beginner as me as well as developer. I have found a nice post too over the internet which also explained very well. For more information of that post check out this link...
http://www.mindstick.com/Articles/6053eec1-02c0-4e09-ab74-c288d8ed41b6/?Connection%20String%20using%20App.config

Thanks everyone for your precious post!
Re: Using app.config file for a console application in C#
#2An Nguyen @ 06/20/13 7:36 AM
"... When deploying, there is one thing to note.
The app.config is a file name used in a project. However, when it is compiled, the file name becomes application's exe name + .config.
For example, if the application name is "sample.exe", then the configuration name should be "sample.exe.config"...."

This is really helpful. I lost an hour to try to see why change in app key value in App.config has no effect. Change should be in XML Configuration file such as "sample.exe" (XML Configuration File). Thanks mucho :))
Re: Using app.config file for a console application in C#
#3Domrol @ 09/13/19 5:07 AM
This section is very Useful, I repeat with my search phase to help some others:

How to use app.config in windows application after release in c#?

When deploying, there is one thing to note.
The app.config is a file name used in a project. However, when it is compiled, the file name becomes application's exe name + .config.
For example, if the application name is "sample.exe", then the configuration name should be "sample.exe.config".

Reply to

Subject:
Name:
E-Mail:
Comment:
(Max 1000 chars)
Confirmation Key:
Please enter the value displayed in the image.
Send Send