Saving Setting for Windows Forms

Saving and retrieving application settings

Every application that I have made needs at minimum two things, that we will put on the generic term “ settings “:

1. “remember” their state – information that are read at the beginning, write at the end of the program . We will name those “local settings” because there are different by each user that uses the application.

2. Connect with other applications (databases, web services, other programs ) and remember the “connection” settings – information that is read and does not change often ( number of writes is minimized). We will name this “global settings”.Most of the time the writing is realized by setup and/or a new application dedicated to application administration.

Usually the settings are saved in a file persisted on local hard disk. From the programmer perspective, there are four questions that arise:

1. when to save

2. where to save

3. how to save

4. how to make the information confidential.

When to save

The saving can be done in two separate moments: on installing the application and in running the application. Usually, the global settings(information that does not change often such as database connection) is persisted on application setup and read when the application starts. The local settings information (such as last action of the user) is read when the application starts and it is write at the application end.

Where to save

For a desktop application there are two places to save the data for a application : the folder where the applications is installed by the user (usually %programfiles% , can be obtained in .NET with Path.GetDirectoryName( Assembly.GetEntryAssembly().GetName().CodeBase)) and the local folder for application data for this user (%USERPROFILE%\AppData\Local – obtained in .NET with
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
). The first one is for global settings, the other one for local user settings.

How to save

We will enumerate here several methods, remaining for you the task to choose between the best. We will deliberately skip the method in which you use a file and you wrote information there in a proprietary format.
In the next examples we will save/retrieve two named properties : DatabaseConnection and LastActionSave method 1 : from Settings within Visual Studio
You can create settings for your project very easy. Load your project in Visual Studio, click on toolbar Project=>Properties, select the tab “Settings” and create the settings. In the picture you will see two settings, one with user scope and one with application scope:
By default Visual Studio creates a class named “Settings” and puts code for retrieving/storing the settings that you create. More – the application settings does not have a “set” by default – it is normal since it is stored in application config file – an ordinary user cannot write this setting.
The code for retrieving/saving the settings is the following:
Console.WriteLine(Properties.Settings.Default.DatabaseConnection);
Console.WriteLine(Properties.Settings.Default.LastAction);
Properties.Settings.Default.LastAction = “difference”;
Properties.Settings.Default.Save();
//you can reset the values by
//Properties.Settings.Default.Reset();
For more details please refer to the following links :

You can read/save directly from application configuration file. Add an application configuration file to the project and a reference to System.Configuration . In the app.config file add
<appSettings>
<add key=”DatabaseConnection” value=”name=Stargate”/>
<add key=”LastAction” value=”add”/>
</appSettings>
And the code is the following:
var app=ConfigurationManager.AppSettings;
foreach (var key in app.AllKeys)
{
Console.WriteLine(string.Format(“{0}={1}”,key,app[key]));
}
Observation 1 : Please ensure you put in the app.config file file just global settings. Otherwise the application must have administrative rights to modify the app.config file.
Observation 2 : For asp.net applications , the modifying of Web.config will result in restarting the application – that is NOT a good behaviour.

Summary

In this chapter we have shown different ways to store application settings in various file formats. A final recommendation is to store application global data in config file (either web.config, either app.config) and local user in XML file ( in
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
folder). More, for each desktop application please provide a way to edit the config file – and make sure you request elevated privileges.