Omer van Kloeten's .NET Zen

Programming is life, the rest is mere details

News

Note: This blog has moved to omervk.wordpress.com.

Omer van Kloeten's Facebook profile

Omer has been professionally developing applications over the past 8 years, both at the IDF’s IT corps and later at the Sela Technology Center, but has had the programming bug ever since he can remember himself.
As a senior developer at NuConomy, a leading web analytics and advertising startup, he leads a wide range of technologies for its flagship products.

Get Firefox


powered by Dapper 

.NET Resources

Articles :: CodeDom

Articles :: nGineer

Culture

Projects

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);
			}
		}
	}
}

Comments

No Comments