App.Config in C#

  1. Adding Connection Strings to App.config in C#
  2. Adding App Settings to App.config in C#
  3. Full C# App.Config Example
App.Config in C#

This tutorial will explain the App.Config file in a C# project and demonstrate how it can be used.

App.Config, or the Application Level Configurations file, is an XML-based file containing the predefined configuration sections available and allows for custom configuration sections that you can modify. It is often automatically generated when creating a new project.

A very basic App.Config can look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
</configuration>

As you can see, it only specifies the version of .NET that will be supported during runtime. From here, you can customize how your application locates and loads assembly files. A typical example of what modifications are done to the App.Config would be storing connectionStrings or appSettings to be accessed throughout the entire project.

After setting up the App.Config file the way you want it, you can call its stored values through the ConfigurationManager. To use the ConfigurationManager, you must add the using statement for System.Configuration to your code.

Adding Connection Strings to App.config in C#

To add a connection string, you must ensure that the section for connectionStrings is present. Then within it, you can add the connection string to be used and the provider name, whose value should be the corresponding namespace for its connection string.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="myConnectionString"
			connectionString="Data Source =MSI\SQLEXPRESS; Initial Catalog =SampleDB; Integrated Security=True"
			providerName="System.Data.SqlClient"
        />
    </connectionStrings>
</configuration>

To access this connection string in your code, you can use the ConfigurationManager and access the entire ConnectionStrings collection. From there, you can pull the desired connection string by its name.

string connectionString =
    ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

Adding App Settings to App.config in C#

To add an app setting configuration, you must ensure that the appSettings section is present. Then within it, you can add a new app setting by providing the key it will be called by and its value.

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

To access this configuration, you can use the ConfigurationManager and access the entire AppSettings collection using the syntax below.

var tableName = ConfigurationSettings.AppSettings["TableName"];

Full C# App.Config Example

Now that we have discussed some of the concepts of App.Config files, we will combine them into an example.

XML:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
	<connectionStrings>
		<add name="myConnectionString"
			connectionString="Data Source =MSI\SQLEXPRESS; Initial Catalog =SampleDB; Integrated Security=True"
			providerName="System.Data.SqlClient"
        />
	</connectionStrings>
	<appSettings>
		<add key="TableName" value="SampleTable" />
	</appSettings>
</configuration>

In the App.Config file, we stored a single connection string and a configuration named TableName. Please take note of their different sections and their different syntax.

C#:

using System;
using System.Configuration;

namespace AppConfig_Example {
  class Program {
    static void Main(string[] args) {
      // Pull the connection string from the App.Config File
      string connectionString =
          ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

      // Pull the app setting from the App.Config File
      var tableName = ConfigurationSettings.AppSettings["TableName"];

      // Print the values to the console
      Console.WriteLine("Configuration String: " + connectionString + "\n");
      Console.WriteLine("Table Name: " + tableName);

      Console.ReadLine();
    }
  }
}

We used the ConfigurationManager to pull the values stored in the App.Config file in the program code. To demonstrate that they were stored and pulled correctly, their values are then printed to the console.

Output:

Configuration String: Data Source =MSI\SQLEXPRESS; Initial Catalog =SampleDB; Integrated Security=True
    
Table Name: SampleTable