Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

AppSettings Implementation for Compact Framework

, 23 Mar 2004
Learn how to create a settings class that will make it easy to access and store your application settings.
savesettings.zip
SaveSettings
bin
Debug
Release
obj
Debug
temp
TempPE
Release
SaveSettings.exe
SaveSettings.Form1.resources
SaveSettings.projdata
temp
TempPE
SaveSettings.csdproj
SaveSettings.csdproj.user
savesettings_src.zip
SaveSettings.exe
SaveSettings.exe
SaveSettings.pdb
SaveSettings.projdata
SaveSettings.exe
SaveSettings.Form1.resources
SaveSettings.projdata
SaveSettings.csdproj
SaveSettings.csdproj.user
SaveSettings.suo
using System;
using System.Collections.Specialized;
using System.IO;
using System.Xml;

namespace SaveSettings
{
	/// <summary>
	/// Summary description for Settings.
	/// </summary>
	public class Settings
	{
		private static NameValueCollection m_settings;
		private static string m_settingsPath;
        
		static Settings()
		{
			m_settingsPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
			m_settingsPath += @"\Settings.xml";
			
			if(!File.Exists(m_settingsPath))
				throw new FileNotFoundException(m_settingsPath + " could not be found.");

			System.Xml.XmlDocument xdoc = new XmlDocument();
			xdoc.Load(m_settingsPath);
			XmlElement root = xdoc.DocumentElement;
			System.Xml.XmlNodeList nodeList = root.ChildNodes.Item(0).ChildNodes;

			// Add settings to the NameValueCollection.
			m_settings = new NameValueCollection();
			m_settings.Add("ServerIP", nodeList.Item(0).Attributes["value"].Value);
			m_settings.Add("UserName", nodeList.Item(1).Attributes["value"].Value);
			m_settings.Add("Password", nodeList.Item(2).Attributes["value"].Value);
			m_settings.Add("PhoneNumber", nodeList.Item(3).Attributes["value"].Value);
			m_settings.Add("TimeOut", nodeList.Item(4).Attributes["value"].Value);
			m_settings.Add("LastTransmit", nodeList.Item(5).Attributes["value"].Value);
			m_settings.Add("DatabasePath", nodeList.Item(6).Attributes["value"].Value);
		}

		public static void Update()
		{
			XmlTextWriter tw = new XmlTextWriter(m_settingsPath, System.Text.UTF8Encoding.UTF8);
			tw.WriteStartDocument();
			tw.WriteStartElement("configuration");
			tw.WriteStartElement("appSettings");

			for(int i=0; i<m_settings.Count; ++i)
			{
				tw.WriteStartElement("add");
				tw.WriteStartAttribute("key", string.Empty);
				tw.WriteRaw(m_settings.GetKey(i));
				tw.WriteEndAttribute();

				tw.WriteStartAttribute("value", string.Empty);
				tw.WriteRaw(m_settings.Get(i));
				tw.WriteEndAttribute();
				tw.WriteEndElement();
			}

			tw.WriteEndElement();
			tw.WriteEndElement();

			tw.Close();
		}

		public static string ServerIP
		{
			get { return m_settings.Get("ServerIP"); }
			set { m_settings.Set("ServerIP", value); }
		}

		public static string UserName
		{
			get { return m_settings.Get("UserName"); }
			set { m_settings.Set("UserName", value); }
		}

		public static string Password
		{
			get { return m_settings.Get("Password"); }
			set { m_settings.Set("Password", value); }
		}

		public static string PhoneNumber
		{
			get { return m_settings.Get("PhoneNumber"); }
			set { m_settings.Set("PhoneNumber", value); }
		}

		public static string TimeOut
		{
			get { return m_settings.Get("TimeOut"); }
			set { m_settings.Set("TimeOut", value); }
		}

		public static string LastTransmit
		{
			get { return m_settings.Get("LastTransmit"); }
			set { m_settings.Set("LastTransmit", value); }
		}

		public static string DatabasePath
		{
			get { return m_settings.Get("DatabasePath"); }
			set { m_settings.Set("DatabasePath", value); }
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

pbrooks
Software Developer (Senior)
United States United States
Check out my blog! http://www.pagebrooks.com

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 24 Mar 2004
Article Copyright 2004 by pbrooks
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid