Application Configuration

• In the live projects development, we may require to offer one or more customizable values in the code.
• For example, you can take a db server name. At the development time, we use the db server in the s/w company. But when the project is issued to the customer, the project should work under the db server, in the customer’s company.
• At that, there should a facility to customize the server name, after installing the project in the customer’s company.
• But, we doesn’t give the source code to the customer, so that the customer can’t change the db server name.
• To solve this problem, this kind of application settings like this, would be maintained in another file separately, which can be modified on the client system, even after installing the project in the customer’s work station.
• In .NET Framework, the application settings can be saved in a config file.
• A config file contains “.config” extension.
• In Console and Windows Applications, it is called as “App.Config”.
• In Web Sites, it is called as “Web.Config”.
• The config file is written is “xml language”.

Implementation of “Config File”
 Add the configuration file.
• Click on “Project” menu – “Add New Item”.
• Select “Application Configuration File”.
• Click on OK.
 Then “App.config” file will be created.
 In the <config> tag, add the <appSettings> tag as follows.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
</appSettings>
</configuration>

 In the “<appSettings>” tag, use “<add>” tag to declare the actual application configuration setting values.
Syn: <add key=”name of the key” value=”actual value”>
Ex: <add key=”servername” value=”myserver”>
Note: The “key” is used to access the configuration setting value in the code.
 Access the configuration setting value with the help of “key”.
System.Configuration.ConfigurationSettings.AppSettings["key"];
It returns the value of the configuration setting, based on the given key in string mode.

Demo on “App.Config”

App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="servername" value="myserver"/>
</appSettings>
</configuration>
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace AppConfigDemo
{
class Program
{
static void Main(string[] args)
{
string server = ConfigurationSettings.AppSettings["servername"];
Console.WriteLine("Connecting with " + server + "....");
Console.Read();
}
}
}
Output:
Config