
.NET 2.0+Application Settings (2)
There are various ways to hold application-wide and user-specific settings externally to a program. One is to set up application settings in a Visual Studio project. These permit strongly typed configuration that can be changed without recompiling.
Reading Application Settings
Creating the default application settings file adds a new namespace, named "Properties", within the project's default namespace. Inside the new namespace is a class that provides access to the new properties. To demonstrate, return to the code editor for the class containing the program's Main method. To simplify access to the new namespace, add the following using directive to the top of the code:
using AppSettings.Properties;
We can now output the contents of the application setting by accessing a property of a class that has been generated by Visual Studio. To output the text to the console, add the following code within the Main method:
Console.WriteLine(Settings.Default.TestSetting);
Console.ReadLine();
Execute the program to see the value from the application setting outputted to the console. You can also try executing the compiled executable and modifying the value within the AppSettings.exe.config file. This will show that the value can be changed without recompiling.
Creating User Settings
If you try to assign a new value to an application-wide setting, the code fails to compile. To see this, modify the code within the Main method as follows. When you try to compile, an error is displayed indicating that the property is read-only.
Console.WriteLine(Settings.Default.TestSetting);
Settings.Default.TestSetting = Console.ReadLine();
Application-wide values may not be changed at run-time. However, user-specific settings can be modified at run-time for an individual user. To change the TestSetting to be user-specific, return to the project's properties and change the Scope value for the setting to "User". The code will now compile but changes to the settings will exist only until the application stops executing. To persist user settings between sessions, you must use the Save method. To try this, add the following line to the end of the Main method:
Try executing the compiled executable version of the program several times, each time entering a new string for the setting. You should see that the entered value is persisted between executions of the software. However, if you review the AppSettings.exe.config file in the executable's folder, you will see that the settings are not being changed. This file continues to hold the default values for each configurable item, ensuring that new users of the program will not inherit changes made by other users. The user-specific values are stored within a new file within the user's folder. The exact path to the folder is determined by the operating system.
Resetting User Settings
In some applications, particularly those with a large number of configurable settings, it is useful to give the user the option to reset all user-specific settings to their default values. You can achieve this using the Reset method. After resetting, the user's configuration file is emptied so that the main configuration file's values are used instead.
Settings.Default.Reset();
10 November 2009