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

Saving and Restoring Application Settings

Rate me:
Please Sign up or sign in to vote.
4.78/5 (38 votes)
15 Mar 20034 min read 128K   3.4K   85   12
The Savior class makes it simple to save and restore application settings using the registry or a binary file.

Sample Image - DemoScreenShot.gif

Introduction

This article introduces a class that simplifies the process of saving and restoring application settings. The Savior class has four static methods that enable saving and reading application settings using the system registry or a binary serialization file. All the methods work with a user-defined object that encapsulates all of the settings and their default values. In the examples, this user-defined class is called Settings. To save the application settings to the registry, the programmer simply sets any values in the Settings object and then calls the Savior.Save(settings) method. Reading the settings is equally simple, as is reading or saving to a file.

Background

Saving and restoring application settings is a tedious part of almost any software project. The .NET Framework provides a simple facility for binary serialization to and from a file. Unfortunately, the registry is more cumbersome, especially for data types other than strings, integers, and byte arrays. The Savior class uses reflection to automatically save and restore objects to the registry, as well as providing a thin wrapper around the binary serialization methods to simplify this process as well.

For example, to save a Font object to the registry without Savior, one has to figure out a method to store the necessary information in strings, integers, or bytes and then read it back later. Using Savior, one simply defines a Font object within the Settings class and the rest is handled automatically.

Savior supports the following data types. Additional types can easily be added as needed.

Supported data types:

  • string
  • bool
  • decimal
  • int
  • float
  • double
  • Color
  • Point
  • Size
  • Font
  • DateTime
  • TimeSpan
  • int[]
  • byte[]
  • string[]
  • bool[]
  • float[]
  • double[]

as well as any enum class.

Using the code

The demonstration project gives an elaborate example showing the use of many different data types. Here we will just show how to save and restore the location and background color of a form.

First, create a Settings class containing the desired data:

C#
[Serializable]
public class Settings
{
    public Color BackColor = Color.Aqua;
    public Point Location = new Point(100,100);
}

Note that both BackColor and Location are given default values within the class definition. Also note that the class has been given the [Serializable] attribute. This is necessary for file serialization.

Saving the location and color of MyForm to the registry now requires only the following code:

C#
// Create an instance of the settings class if it does not already exist
Settings settings = new Settings();

// Store some information in the Settings object
settings.BackColor = MyForm.BackColor;
settings.Location = MyForm.Location;

// Now save everything to a registry key
Savior.Save(settings,"Software\\Ultrapico\\Savior");

In this case, one of several overloaded versions of the Savior.Save() method is used. This one stores the information in HKEY_CURRENT_USER\Software\Ultrapico\Savior.

Reading settings is equally simple:

C#
// Read settings from a registry key
Savior.Read(settings,"Software\\Ultrapico\\Savior");

// Update the application settings
MyForm.BackColor = settings.BackColor;
MyForm.Location = settings.Location;

To save and restore using a binary serialization file, use the following code:

C#
// Save settings to a file
Savior.SaveToFile(settings,FileName);

...

// Read settings from a file
settings = (Settings)Savior.ReadFromFile(FileName);

Note the different syntax of the ReadFromFile() and Read() methods. The ReadFromFile() method returns a Settings object, but it must be explicitly cast to the proper type.

Summary of methods

Here is a complete list of the methods provided by the Savior class:

  • void Save(Settings) --> Saves the settings to the default registry key
  • void Save(Settings,string) --> Saves the settings to a specified key name in HKCU
  • void Save(Settings,RegistryKey) --> Saves the settings to a specified registry key
  • void SaveToFile(Settings,string) --> Saves the settings to a specified file
  • void Read(Settings) --> Reads the settings from the default registry key
  • void Read(Settings,string) --> Reads the settings from a specified key name in HKCU
  • void Read(Settings,RegistryKey) --> Reads the settings from a specified registry key
  • object ReadFromFile(string) --> Reads the settings from a specified file
  • string ToString(Settings) --> Returns information about the settings

Under the hood

Savior makes extensive use of object reflection to read and save data to the registry. Here is a slightly simplified excerpt from the Save() method illustrating how it works:

C#
foreach(FieldInfo fi in settings.GetType().GetFields())
{ 
    // Test the name of the Field type, converted to lower case
    switch (fi.FieldType.Name.ToLower())
    {
        // strings are a native registry data type, so they are easy to save
        case "string":
            Key.SetValue(fi.Name,(string)fi.GetValue(settings));
            break;

        // Store a Point as two separate integers
        case "point": 
            Point point=(Point)fi.GetValue(settings);
            Key.SetValue(fi.Name+".X",point.X);
            Key.SetValue(fi.Name+".Y",point.Y);
            break;
                    
        // Saving colors is easy, unlike reading them, which is trickier. 
        // We just use the Color's
        // Name property. If there is no known name, 
        // the Argb value will be written in hexadecimal.
        case "color":
            Key.SetValue(fi.Name,((Color)fi.GetValue(settings)).Name);
            break;
                    
        ...

The method uses reflection to iterate through all fields in the Settings class. The type of each field is used to determine how to save the data in the registry. For example, as the code above shows, a Point named Location would be stored in the registry as a pair of string values whose names are Location.X and Location.Y. The Read() method works in a similar way.

The demonstration program

The demonstration program shows how numerous settings can be saved and restored using Savior. Among other things, it shows how to save the settings of a ListView control including the column headings, column order, and column widths. The tricky part of this is keeping track of the column order, which is done using Interop. Special thanks to dfontanesi on The Code Project, for his article "Persisting ListView settings with serialization". The ListViewSettings class used in the demonstration project uses a simplified version of his class.

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
Researcher
United States United States
Ultrapico Website: http://www.ultrapico.com

Download Expresso 3.0, the latest version of the award-winning regular expression development tool.

Comments and Discussions

 
GeneralVersioning Pin
JPark23-Mar-03 16:56
JPark23-Mar-03 16:56 
GeneralRe: Versioning Pin
Jim Hollenhorst23-Mar-03 19:40
Jim Hollenhorst23-Mar-03 19:40 

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.