Storing Application Specific Information in NET

Application settings have traditionally been stored in .ini files, in a database, or in the registry. Among those traditional choices, INI files are the most popular for traditional VB applications.

INI files have gone by the wayside in .NET. This is a good thing, in my opinion. Writing external settings code or reading .ini files is a tedious task. .NET uses .config files rather than INI files.

In this entry, find out how to use .config files in .NET for application specific settings.Bye, Bye INI

INI files have gone by the wayside in .NET. This is a good thing, in my opinion. Writing external settings code or reading .ini files is a tedious task. .NET uses .config files rather than INI files.

Config files are similar to INI files because they are specific to an application and are stored locally but the .config file is more powerful than a text based INI file.

All .NET applications can have a .config file, but not all applications necessarily have one. The name of an application’s .config file depends on the type of application.

Web applications and Web services have the filename web.config. In Visual Studio .NET 2002, the web.config file is the only application configuration file that Visual Studio .NET automatically generates and populates with information automatically.

For WinForms applications, you will need to create the .config file and place it in the appropriate location.

Creating the .config file

The .config file is written in xml and is stored in the same directory as the application’s executable. The application’s full filename must also be the configuration file’s name followed by .exe.config. For example, if your application name is myApplication, then the config file for the application is:myAapplicatione.exe.config

The major advantages of using .config files are that all the application's configuration settings are in one location and you don't need to write any code to install the settings. You simply need to place the file in the appropriate location.

.NET makes retrieving application configuration settings easy by providing the ConfigurationSettings class. The ConfigurationSettings class contains a shared AppSettings property that allows you to read configuration settings in the tag in the config file for your project.

Here is a sample application configuration file that you could use to perform a common application setting…the connection string for your application:

Why use .config files? If you developed an application to access a database on a SQL Server and you hard-code the connection information in the application, what happens if you want to use a different database server?

If the connection information is hard-coded in the application, you’ll have to modify, recompile, and redeploy the application to make this make the application point to the correct database.

Using a .config file, you simply change the ConnectionString key in the .config file.

Disclaimer: Blog contents express the viewpoints of their independent authors and
are not reviewed for correctness or accuracy by
Toolbox for IT. Any opinions, comments, solutions or other commentary
expressed by blog authors are not endorsed or recommended by
Toolbox for IT
or any vendor. If you feel a blog entry is inappropriate,
click here to notify
Toolbox for IT.

Follow one Software Developer with extensive experience engineering end-user applications as she helps an international ...
more

Follow one Software Developer with extensive experience engineering end-user applications as she helps an international manufacturing company move a mission critical scheduling and materials management application from VBA to .NET, sharing various challenges, processes, and best practices she encounters along the way.
less

Receive the latest blog posts:

Share Your Perspective

Share your professional knowledge and experience with peers. Start a blog on Toolbox for IT today!