Click here to Skip to main content
15,888,527 members
Articles / Programming Languages / C#

SwitchNetConfig - Laptop users, quickly switch network and proxy configuration in different places

Rate me:
Please Sign up or sign in to vote.
4.66/5 (75 votes)
6 May 2004CPOL2 min read 559.4K   22.1K   199  
A handy utility for laptop users which stores network and proxy configuration as profiles and apply a profile very quickly whenever laptop goes to a different network
using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace SwitchNetConfig
{
	/// <summary>
	/// Helper class for persisting configuration
	/// </summary>
	public class ConfigurationHelper
	{
		#region Variables

		private const string PROFILE_FILE_NAME = "Profiles.xml";

		#endregion

		#region Public Static 

		/// <summary>
		/// Loads the collection of profiles from configuration file
		/// </summary>
		/// <returns></returns>
		public static ArrayList LoadConfig()
		{
			if( File.Exists( PROFILE_FILE_NAME ) )
			{
				// Use XML Deserializer to load the serialized collection
				XmlSerializer serializer = new XmlSerializer( typeof( ConfigWrapper ) );
				// read the profile file
				StreamReader reader = new StreamReader( PROFILE_FILE_NAME );
				// deserialize the profile collection from the file
				ConfigWrapper wrapper = (ConfigWrapper) serializer.Deserialize( reader.BaseStream );
				// close the file handle
				reader.Close();
				// return the profile collection
				return wrapper.Profiles;
			}
			else
				return new ArrayList();

		}
		/// <summary>
		/// Saves the profiles in the configuration file.
		/// </summary>
		/// <param name="profiles"></param>
		public static void SaveConfig( ArrayList profiles )
		{
			// Use XML Serializer to serialize the content of the specified array list
			XmlSerializer serializer = new XmlSerializer( typeof( ConfigWrapper ) );
			// open the profile file
			StreamWriter writer = new StreamWriter( PROFILE_FILE_NAME, false );				
			try
			{
				ConfigWrapper wrapper = new ConfigWrapper();
				wrapper.Profiles = profiles;
				
				// Serialize the array list to the file
				serializer.Serialize( writer.BaseStream, wrapper );
			}
			catch( Exception x )
			{
				System.Diagnostics.Debug.WriteLine( x.Message );
			}
			// close the file
			writer.Close();
		}

		#endregion
	}

	#region ConfigWrapper

	/// <summary>
	/// Configuration wrapper class
	/// </summary>
	[XmlInclude( typeof( Profile ) )]
	public class ConfigWrapper
	{
		[XmlArrayAttribute("Profiles")]
		public ArrayList Profiles;
	}

	#endregion

}

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)


Written By
Architect BT, UK (ex British Telecom)
United Kingdom United Kingdom

Comments and Discussions