1.9.09

How to use Configuration file in DOTNET

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.

* To store values in the configuration file, you can create XML elements in the format:
?xml version="1.0" encoding= "utf-8" ?
configuration

appSettings
add key="DatabasePath" value="c:\\projects\data\spider.mdb"
add key="SupportEmail" value="webmaster-1@dotnetspider.com"
appSettings

configuration

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

string dbPath =
System.Configuration.ConfigurationSettings.AppSettings["DatabasePath"];
string email =
System.Configuration.ConfigurationSettings.AppSettings["SupportEmail"];

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"];
string email = ConfigurationSettings.AppSettings["SupportEmail"];

In VB.NET, you have to use "( ... )" instead of the "[ ... ]"

Note: When you compile your application, VS.NET will automatically create a file called .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 .exe.config and NOT the app.config. Users can modify the data in .exe.config file and application will read the data from the config file, when restarted.

web.config for Windows applications

The web applications use the same concept, but they use a config file with the name web.config. There are couple of things to note in this case.

web.config is created automatically by VS.NET when you create any web project.
When you compile the web application, web.config is NOT renamed or copied to the BIN folder.
web.config has several default entries in it to support web/IIS configuration & security.
You can add the section in the web.config and add your key/value pairs in that section.
You can have separate web.config files for each directory in your web application, in addition to the one in the root. For each web page, by default, system will look for a web.config in the same folder as the page and if not found, then looks in the parent folder.

No comments: