Tutorials » AspNet tutorials »

App.config for Windows applications

This chapter explains the App.config file used for Windows applications

app.config for Windows applications

Windows applications in VS.NET use the name app.config by default for the configuration file. This will not be automatically created when you create a Windows application. If you need a configuration file for your application, open your project in VS.NET, go to the 'Solution Explorer' and right click on the project name. Choose Add > Add new item from the menu and select 'Application Configuration file' from the list of choices. This will create an app.config file for you in the application root.

By default, the app.config file will have the following content:
<?xml version="1.0" encoding="utf-8" ?>



To store values in the configuration file, you can create XML elements in the format:

<add key="MyKey" value="MyValue" />

See the sample config entries below:

<?xml version="1.0" encoding= "utf-8" ?>



<add key="DatabasePath" value="c:\\projects\data\tutorials.mdb" />



And to read from this config file, just use the following code in your application:

dim dbPath as string =

ConfigurationSettings is the class used to access the contents of the configuration file. Since this class is part of the namespace System.Configuration, we have to use the fully qualified name System.Configuration.ConfigurationSettings. As a shortcut, you can use the using directive on top of the file like below:

using System.Configuration;

If you have the above directive on top of the file, then you can directly use the class ConfigurationSettings.

string dbPath = ConfigurationSettings.AppSettings("DatabasePath");

In C#, you have to use "[ ]" instead of the "( )", as shown below:

string dbPath = ConfigurationSettings.AppSettings["DatabasePath"]

Note: When you compile your application, VS.NET will automatically create a file called <your application name>.exe.config in your bin\debug folder. The contents of the app.config will be automatically copied to this new config file when you compile the application. When you deliver the application to the end user, you have to deliver the exe and this new config file called <your application name>.exe.config and NOT the app.config. Users can modify the data in <your application name>.exe.config file and application will read the data from the config file, when restarted.

By default, you can have only one configuration file for a windows application. If you need to read from more than one config file, you have to do some additional coding.

Next Chapter: Web.config for Web applications
Previous Chapter: Configuration File for .NET applications
More Chapters: ASP.NET Tutorials
More Tutorials: Tutorial Index

Top Contributors
    Last 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India