Click here to Skip to main content
13,894,768 members
Click here to Skip to main content
Add your own
alternative version


83 bookmarked
Posted 15 Mar 2003

Saving and Restoring Application Settings

, 15 Mar 2003
Rate this:
Please Sign up or sign in to vote.
The Savior class makes it simple to save and restore application settings using the registry or a binary file.

Sample Image - DemoScreenShot.gif


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.


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:

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:

// 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

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:

// Read settings from a registry key

// 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:

// Save settings to a file


// 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:

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":

        // Store a Point as two separate integers
        case "point": 
            Point point=(Point)fi.GetValue(settings);
        // 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":

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.


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


About the Author

Jim Hollenhorst
United States United States
Ultrapico Website:

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

You may also be interested in...


Comments and Discussions

GeneralThanks a lot [modified] Pin
ZatriX17-Jun-10 23:41
memberZatriX17-Jun-10 23:41 
GeneralThank You Pin
jimpar25-Feb-09 5:19
memberjimpar25-Feb-09 5:19 
GeneralRe: Thank You Pin
Jim Hollenhorst25-Feb-09 16:44
memberJim Hollenhorst25-Feb-09 16:44 
Generallittle help Pin
kelsayfst30-Jan-07 17:19
memberkelsayfst30-Jan-07 17:19 
GeneralRe: little help Pin
Jim Hollenhorst31-Jan-07 15:49
memberJim Hollenhorst31-Jan-07 15:49 
I still use this code, but I haven't looked at it in 4 years. I think all you need to do is add a "uint32" case to the switch statements in both the Save() and Read() methods. You can pattern these after the other integer cases. It's just a few lines of code.


GeneralListView Multi-Column Re-Order Bug! Pin
GerhardL27-Oct-05 23:39
memberGerhardL27-Oct-05 23:39 
GeneralVersioning Pin
JPark23-Mar-03 16:56
memberJPark23-Mar-03 16:56 
GeneralRe: Versioning Pin
Jim Hollenhorst23-Mar-03 19:40
memberJim Hollenhorst23-Mar-03 19:40 
GeneralStructs Pin
yetanotherchris19-Mar-03 23:58
memberyetanotherchris19-Mar-03 23:58 
GeneralLooks great Pin
John O'Byrne16-Mar-03 22:30
memberJohn O'Byrne16-Mar-03 22:30 
GeneralGreat! Pin
Luis Alonso Ramos16-Mar-03 15:53
memberLuis Alonso Ramos16-Mar-03 15:53 
Generaltwo thumbs up Pin
dog_spawn16-Mar-03 15:08
memberdog_spawn16-Mar-03 15:08 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 16 Mar 2003
Article Copyright 2003 by Jim Hollenhorst
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid