Introduction
The application-scoped settings is one of the problems that the NET programmer has when he/she needs to decide where to put those global settings that need to use all users in the application, but also need to change programmatically by determinate user.
Different solutions has been proposed to resolve this issue, one is to create independent read and write methods capable of modifying the configuration XML file programmatically (Levan Midodashvili). The problem with the solutions writing in the configuration file is the VISTA security model. The code cannot write more in the Program File directory without administrative privileges. That mining that the normal Vista user always get the annoying message “are you authorize…” or worst, the system stops the program.
There also exists a more sophisticated solution that makes a mirror of the application-scoped property as user-scope setting (mjmeans). That is a very good solution, but you need to create custom code for each setting that you want to modify programmatically.
The proposed solutions should cover the following requirements:
- The user can programmatically modify the application-scope setting.
- Manual modification in the configuration file must be taken by the setting as the actual value.
- The user should be capable of returning the application-scope setting to the default value in the configuration setting file programmatically.
- The user-scope setting must not be affected.
Limitation of the present version:
- It only tested for string and integer setting values.
- The access to the setting value is not strong typed.
Background
The proposed solution has the following behavior that you can see in figure 1:
As you see, the Initialization of setting Manager gets the information of the existing application-scope settings from the application configuration file. This information is stored in the Setting Manager class.
The user program takes the information about these settings from the Setting Manager. If the user wants to get the default value of the setting, Reset Setting gets the information from the Application configuration file and updates the value to the store value in configuration file.
Initialize Setting Manager, also compare with an internal value if the Application configuration file was changed (manually) and update it with the new value.
That is what we want. Now how to implement it?
The solution is relatively simple. The setting manager stores the values in the assembly isolated store of the application. The Initialization routine gets all application-scoped settings from the configuration application file and stores the values in an XML file that is stored in the assembly isolated storage of the program. You can see an example of this file in the code 1 snippet.
="1.0"="utf-8"
<settings>
<setting name="height" type="System.Int32">
<default>600</default>
<actual>600</actual>
</setting>
<setting name="width" type="System.Int32">
<default>800</default>
<actual>600</actual>
</setting>
<setting name="web" type="System.String">
<default>http://www.g-softsolutions.de</default>
<actual>http://www.codeproject.com</actual>
</setting>
</settings>
As you can see in the example, each setting is store double. The <default>
node stores the original value in the configuration file. And the <actual>
node stores the value entered for the user programmatically. The setting manager always returns the actual node value.
When you run the initialization method of the setting manager, it revises, if the <default>
value coincides with the value in the application configuration file, if the value does not coincide, then the method, updates its <default>
and <actual>
value with the new values.
The result is that you can always synchronize the value of the configuration file and the Setting Manager.
You can see in the following figure how to operate the Initialization procedure of the Setting Manager Class:
That is the theory. In the next section, we can see a simple implementation example of the Setting Manager. This relatively simple implementation can store string and integer values from the setting file.
Using the Code
AppSettingManager
is implemented as a static
class in C#. You can download the code of the class and an example in the attached code.
The interface of AppSettingManager
is relatively simple. It has five public
methods:
public static void InitAppSettingManager()
public static string GetApplicationSettingActual(string name)
public static string GetApplicationSettingDefault(string name)
public static void SetApplicattionSettingActual(string appSettingName, string value)
public static void SetApplicattionSettingActual(string appSettingName, string value)
To use the class, you should integrate it in your code. That is because the code interacts with your application configuration setting class.
In the example, you can see a Windows Form with a web explorer object. The configuration setting file stores the default URL, the height and the form width as application-scoped setting.
In Form constructor, the Setting manager is initialized and the values are taken from it as you can see in the following code snippet:
public Form1()
{
InitializeComponent();
AppSettingManager.InitAppSettingManager();
textBox1.Text = AppSettingManager.GetApplicationSettingActual("web");
var address = new Uri(AppSettingManager.GetApplicationSettingActual("web"));
web1.Url = address;
Height = int.Parse(AppSettingManager.GetApplicationSettingActual("height"));
Width = int.Parse( AppSettingManager.GetApplicationSettingActual("width"));
textBox2.Text = AppSettingManager.GetApplicationSettingActual("width");
BackColor = Properties.Settings.Default.color;
}
In the following snippet, you can see how to write an application-scope setting using the Setting Manager:
private void button1_Click(object sender, EventArgs e)
{
AppSettingManager.SetApplicattionSettingActual("web", textBox1.Text);
var address = new Uri(AppSettingManager.GetApplicationSettingActual("web"));
web1.Url = address;
}
To test it, open the program, you can see that as default the URL setting is to http://www.g-softsolutions.de.
Enter in the textbox “web” the URL of The Code Project for example, and click the Get Setting Button. That copies the new value in the XML setting file. The program also updates the Web control URL as you can see:
Close the program and reopen it. You can see that now the default URL is the URL that you entered (in this case the URL of the home page of The Code Project. If you click Get Default, then the default value from the application configuration file is used as setting value.
You can also test with the width property.
The rest of the code is trivial.
Points of Interest
The use of isolated storage to avoid the limitation of writing the application file which can be a very useful possibility if you need to programmatically modify your Application-scoped settings. (for example the Connection String). The advantages to use this method are the following:
- You can write and read the settings without limitation.
- You don’t need to create custom code for different settings.
- You can use the code without giving special privileges to the user or to the code.
- You can always return to the default value in the application configuration file.
- You can also introduce new settings and change them manually in the application configuration file. The changes are recognized by the Manager (you only need to run the method
InitAppSettingManager
.
Limitations
- At the moment, only string and number are accepted by the example implementation.
To Do
- Extend the manager implementation to manage different settings object
History