Click here to Skip to main content
15,886,258 members
Articles / Programming Languages / C#
Article

SettingsXpress - Application Settings Made Easy

Rate me:
Please Sign up or sign in to vote.
4.67/5 (11 votes)
10 May 20032 min read 84.6K   842   35   24
Application Settings Storage and Retrieval Simplified

Introduction

With SettingsXpress you can easily create an XML settings file for your application, to store and retrieve application settings, using a streamlined code interface.

SettingsXpress can store and retrieve all native CLS compliant DotNet data types as well as Point, Size, Color, Guid, DateTime and TimeSpan settings without the need to parse string values.

The SettingsFile class acts as a virtual storage facility for your application and component settings. The settings file is organized in a hierarchical format, based on a logical ordering of SettingsKeys stored within it.

By default the settings file is stored in the current users AppData folder for the current client application. You can also specify a custom location and filename for the settings file using the SettingsFile.Create method.

A SettingsKey is the base unit of organization in the settings file, and can be compared to a folder. A particular key can have subkeys (just as a folder can have subfolders)

Each SettingsKey can have multiple settings stored within it. Each setting holds one particular value, which can be retrieved or updated when required.

Using SettingsXpress

Creating SettingsKeys and storing settings

To use your application settings file, you start by accessing the static Settings property of the SettingsFile class, which returns the root SettingsKey in the settings file.

To create a subkey use the CreateSubKey method. Once a SettingsKey is established you can now store settings using the StoreSetting method.

C#
SettingsKey dataSettings = SettingsFile.Settings.
            CreateSubKey("DataManager/Connection");
dataSettings.StoreSetting("DriverType", 
            "Microsoft Excel");

Note: When you first access the settings file in your code, it is automatically loaded or created. To specify a specific location for the settings file use the SettingsFile.Create method.

The above code generates the XML settings file as follows :

XML
<Settings>
    <DataManager>
      <Connection DriverType = "Microsoft Excel"/>
    </DataManager>
</Settings> 

Once you have settings established in the settings file, use the SettingsFile.Update method to update the settings file.

Retrieving SettingsKeys and settings

To retrieve a SettingsKey in the settings file you have the following options:

  • Use the SettingsKey.OpenSubkey method.
  • Use the SettingsKey[path] indexer.
  • Use the SettingsKey.CreateSubKey method

Tip : The SettingsKey.CreateSubkey method is a good option to use, when you are not certain if the key is yet to establish in the settings file.

To retrieve a setting stored within a SettingsKey, use the SettingsKey's GetSetting, GetPoint, GetSize, or GetColor methods.

The following code retrieves the width of a TreeView from the settings file.

C#
SettingsKey TreeViewSettings = SettingsFile.
                Settings["UI/MainWindow/TreeView"];
int width = TreeViewSettings.GetSetting
            ("Width",this.TreeView.Width);

History

  • 04/20/2003 - Test release v0.5
  • 05/11/2003 - Release v1.1 with source

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGetColor Problem Pin
qstar2k28-Apr-08 7:50
qstar2k28-Apr-08 7:50 
GeneralSadly, contact email in source is also dead Pin
Perry28-Mar-06 14:22
Perry28-Mar-06 14:22 
QuestionRe: Sadly, contact email in source is also dead Pin
Amber Star8-Mar-06 23:34
Amber Star8-Mar-06 23:34 
AnswerRe: Sadly, contact email in source is also dead Pin
Perry29-Mar-06 6:21
Perry29-Mar-06 6:21 
AnswerRe: Sadly, contact email in source is also dead Pin
Amber Star9-Mar-06 11:03
Amber Star9-Mar-06 11:03 
GeneralRe: Sadly, contact email in source is also dead Pin
Perry210-Mar-06 7:53
Perry210-Mar-06 7:53 
GeneralWrong contact info url Pin
Perry28-Mar-06 7:45
Perry28-Mar-06 7:45 
QuestionSource formatting is not MS-Windows? Pin
Perry27-Mar-06 8:05
Perry27-Mar-06 8:05 
GeneralDeleting a section Pin
Member 107786328-May-04 11:32
Member 107786328-May-04 11:32 
GeneralRe: Deleting a section Pin
Member 107786328-May-04 12:14
Member 107786328-May-04 12:14 
GeneralRe: Deleting a section Pin
Amber Star28-May-04 12:41
Amber Star28-May-04 12:41 
GeneralRe: Deleting a section Pin
Member 107786328-May-04 12:46
Member 107786328-May-04 12:46 
GeneralGreat Solution Pin
pestrada11-Mar-04 3:16
pestrada11-Mar-04 3:16 
QuestionDoc? Pin
Steven Carleton18-Jul-03 14:21
Steven Carleton18-Jul-03 14:21 
QuestionSpaces in key names? Pin
baldo1-Jul-03 22:19
baldo1-Jul-03 22:19 
AnswerRe: Spaces in key names? Pin
Amber Star2-Jul-03 10:31
Amber Star2-Jul-03 10:31 
Hi John,

At this time subkeys cannot contain spaces , as XML elements cannot as well. However it would be easy to add this ability by using the XmlConvert.EncodeName Method and the XmlConvert.DecodeName Method. You have two option you could modify the source code, or Encode and Decode in the client before creating the Subkey.



Also you can join the GotDotNet Workspace and add the feature to the Workspace source code. Or simply post it up as a bug on the workspace, either way, We can get this feature in the library.

Do you have any other questions or suggestions? Feel free to post them on the Workspace.. Your input and insight is very very welcome.Roll eyes | :rolleyes:

Thank you,
Amber.

Amber Star
Visit My WebLog..
QuestionCan we have more usage examples please? Pin
TintinAndSnowy13-May-03 19:43
TintinAndSnowy13-May-03 19:43 
AnswerRe: Can we have more usage examples please? Pin
Amber Star14-May-03 14:00
Amber Star14-May-03 14:00 
Generalnunit.framework missing reference Pin
baldo11-May-03 22:59
baldo11-May-03 22:59 
GeneralRe: nunit.framework missing reference Pin
Anonymous12-May-03 7:04
Anonymous12-May-03 7:04 
GeneralSource code is Included Pin
Amber Star11-May-03 11:44
Amber Star11-May-03 11:44 
QuestionTotally awesome -- where's the source? Pin
cwkingjr9-May-03 2:25
cwkingjr9-May-03 2:25 
AnswerRe: Totally awesome -- where's the source? Pin
Jonathan [Darka]9-May-03 2:35
professionalJonathan [Darka]9-May-03 2:35 
GeneralRe: Totally awesome -- where's the source? Pin
peterchen12-May-03 3:37
peterchen12-May-03 3:37 

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

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