Click here to Skip to main content
Click here to Skip to main content

Writing the Read Only Application-scoped Settings

, 21 Jun 2009
Rate this:
Please Sign up or sign in to vote.
A method to programmatically write the application-scoped setting in .NET Framework

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:

WIAS

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.

<?xml version="1.0" encoding="utf-8"?>
<!--Writable mirror of Application Setting in Isolated Storage-->
<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:

wias2.jpg

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:

           
/// <summary>
/// Initialize the Application setting manager.
/// </summary>
public static void InitAppSettingManager()

/// <summary>
/// Get the actual value of a determinate application setting
/// </summary>
/// <param name="name">name of the application setting</param>
/// <returns>Application setting value</returns>
public static string GetApplicationSettingActual(string name)

/// <summary>
/// Get the default value of a determinate application setting
/// The default value is the value in the setting file
/// </summary>
/// <param name="name">name of the application setting</param>
/// <returns>Application setting value</returns>
public static string GetApplicationSettingDefault(string name)

/// <summary>
/// Write a application setting with a value
/// </summary>
/// <param name="appSettingName">Setting Application to alter</param>
/// <param name="value">Value to enter in the setting application</param>
public static void SetApplicattionSettingActual(string appSettingName, string value)

/// <summary>
/// Write a application setting with a value
/// </summary>
/// <param name="appSettingName">Setting Application to alter</param>
/// <param name="value">Value to enter in the setting application</param>
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:

           
/// <summary>
/// Example form Constructor
/// </summary>
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:

                       
/// <summary>
/// Change the "web" application-scoped setting
/// </summary>
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.

wias3.jpg

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:

wias4.jpg

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

  • First version 21.06.2009

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

freedeveloper
Software Developer (Senior) G-SoftSolutions
United States United States
Jose A. Garcia Guirado, Electronic Engineer, graduated in Havana/Cuba 1982, MCTS, MCSD.NET, MCAD.NET, MCSE. Worked in the Institute for Cybernetics and Mathematics of Academy of Science of Cuba for 8 years; since 1995 working as free software architect, developer and advisor, first in Argentina and from 2003 to 2010, in Germany as External consultant in DWS Luxemburg, AIXTRON AG and Shell Deutschland GmbH and from 2010 to 2012 in in Mexico in Twenty Century Fox, and Mexico Stock Exchange (BMV). Actually as freelance in Florida, USA.
Follow on   Google+

Comments and Discussions

 
GeneralExport to other machine [modified] PinmemberYY Ang29-Sep-10 17:18 
GeneralNo need to integrate in code PinmemberRobertoMakoto12-Sep-09 8:11 
GeneralRe: No need to integrate in code Pinmemberfreedeveloper12-Sep-09 11:17 
GeneralRe: No need to integrate in code PinmemberVanguarda14-Sep-09 3:16 
GeneralAlgunas cosas PinmemberNeverbirth16-Jul-09 21:57 
GeneralRe: Algunas cosas Pinmemberfreedeveloper17-Jul-09 11:56 
GeneralRe: Algunas cosas PinmemberNeverbirth17-Jul-09 13:22 
GeneralWinForms Connection Strings at Runtime [modified] PinmemberPat Tormey24-Jun-09 0:42 
GeneralRe: WinForms Connection Strings at Runtime Pinmemberfreedeveloper24-Jun-09 10:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 21 Jun 2009
Article Copyright 2009 by freedeveloper
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid