Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Applications » Downloads
 
Add your own
alternative version
Go to top

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

, 6 May 2004
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)

Share

About the Author


| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 7 May 2004
Article Copyright 2004 by Omar Al Zabir
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid