Storing Application Configuration

Phil Haack‘s words “Do not store user settings in an application configuration file!” reminded me of something I made a while back, in order to store user settings in an application. I used the Object Model Generator to create an object oriented representation of my configuration and wrote a wrapper for it to load from, and persist back to, an Xml based format.
Here’s some code for you:

using System;
using System.IO;
using System.IO.IsolatedStorage;
namespace Utilities.Configuration
{
public class ConfigurationFile
{
private static IsolatedStorageFile m_IsolatedStorage = null;
private static ConfigurationElement m_Element = null;
// This is the name of the configuration file I will use in isolated storage.
private const string ConfigurationFileName = "configuration.xml";
// This class can not be inherited.
private ConfigurationFile()
{
}
// The static constructor loads the file and creates the document object
// model out of it, then stores it in memory for manipulations.
static ConfigurationFile()
{
// Load the configuration file's contents.
string configurationFile = LoadConfigurationFile();
if (configurationFile != null)
{
// If the configuration file existed, create the object grid from it.
ConfigurationDocument document = new ConfigurationDocument();
document.LoadXml(configurationFile);
m_Element = document.CreateDOM();
}
else
{
// If the configuration file has not been found,
// create a new object grid.
m_Element = new ConfigurationElement();
}
}
// Using this property, we can access the configuration settings.
public static ConfigurationElement DOM
{
get
{
return m_Element;
}
}
// This method is called when the application is closed,
// causing it to commit all the changes made to the object
// grid back to the configuration file in the isolated storage.
public static void Close()
{
// Save the configuration file.
SaveConfigurationFile(ConfigurationDocument.FromDOM(m_Element).OuterXml);
// Dispose of the isolates storage file.
if (m_IsolatedStorage != null)
{
m_IsolatedStorage.Dispose();
}
}
private static string LoadConfigurationFile()
{
if (m_IsolatedStorage == null)
{
// This gets storage isolated for this user using this application.
m_IsolatedStorage = IsolatedStorageFile.GetUserStoreForAssembly();
}
// This looks for the file. If it is found,
// it reads its contents and returns that.
if (m_IsolatedStorage.GetFileNames(ConfigurationFileName).Length == 1)
{
using (StreamReader sr = new StreamReader(new IsolatedStorageFileStream(ConfigurationFileName, FileMode.Open, FileAccess.Read, FileShare.Read, m_IsolatedStorage)))
{
return sr.ReadToEnd();
}
}
else
{
return null;
}
}
private static void SaveConfigurationFile(string fileData)
{
// Store the data to its original file location.
using (StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream(ConfigurationFileName, FileMode.Create, FileAccess.Write, FileShare.None, m_IsolatedStorage)))
{
sw.Write(fileData);
}
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s