.NET logo

user settings persistence

home | software | utility classes | user settings persistence

User settings can be stored in various places in .NET. Traditionally, developers would use the registry, or a custom settings file. The IsolatedStorage facility in .NET should not be overlooked for this task. It is accessed via a stream (meaning any serializable object may be written/read) and stored data follows the user via their roaming profile. In secure environments, it may only be accessed by authorised users, and the isolation level can be configured (by user, assembly, domain).

Here we have a simple class designed to simplify the storage and retrieval of basic settings objects, be they strings, numeric values or serializable structures. These objects are keyed by strings, and these strings are used as file names within the roaming profile data. On my system, these files are tucked away here:

\Documents and Settings\<username>\Local Settings\Application Data\IsolatedStorage

You can read more about Isolated Storage.

using the class

An example usage, showing a simple case:

/// <summary> /// A simple settings class (majority of implementation omitted for brevity). /// </summary> [Serializable] private class SettingsData { public string FileName; public int ConnectionLimit; public float LoadRatio; public SettingsData() { // set default values FileName = "DefaultFileName"; ConnectionLimit = 10; LoadRatio = 0.5f; } } private SettingsData _settings; private const string SettingsFileName = "Settings"; private void LoadSettings() { SettingsData settings = (SettingsData)UserSettings.RestoreObject(SettingsFileName); // if nothing loaded, initialise with default settings if (settings==null) settings = new SettingsData(); _settings = settings; } private void SaveSettings() { // write the settings file UserSettings.StoreObject(SettingsFileName, _settings); }


submitting feedback

Please feel free to provide feedback on this class . Bug reports are most appreciated when accompanied by a failing NUnit test case.

Authored by Drew Noakes, February 2005. Use freely, though keep this message in the source intact and report any bugs to me. I also appreciate seeing extensions, or simply hearing that you're using these classes. You may not copyright this work, though may use it in commercial/copyrighted works. Happy coding.