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

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;

namespace SwitchNetConfig
{
	public delegate void StatusUpdate( string message );
		
	/// <summary>
	/// Applies profile
	/// </summary>
	public class ProfileManager
	{
		#region Variables

		private Profile _Profile;

		public event StatusUpdate OnStatusUpdate;

		#endregion

		#region Constructors
		
		public ProfileManager( Profile profile )
		{
			_Profile = profile;
		}

		#endregion

		#region Public Methods

		/// <summary>
		/// Call this to apply the specified profile for the specified NIC
		/// </summary>
		/// <param name="nicName"></param>
		public void Run( string nicName )
		{
			applyNIC( nicName );
			applyIEProfile( _Profile.IEProfile );
		}

		/// <summary>
		/// Apply on all NICs
		/// </summary>
		public void Run( )
		{
			foreach( NICProfile nic in _Profile.NICProfiles )
				Run( nic.Name );
		}

		#endregion

		#region Private methods

		/// <summary>
		/// Applies configuration for the specified NIC
		/// </summary>
		/// <param name="nicName"></param>
		private void applyNIC( string nicName )
		{
			NICProfile nicProfile = getNICProfile( nicName );

			if( null == nicProfile ) return;

			UpdateStatus( "Setting configuration for: " + nicName );

			try
			{
				if( nicProfile.UseDHCP )
					WMIHelper.SetDHCP( nicName );
				else
					WMIHelper.SetIP( nicProfile.Name, nicProfile.IP, nicProfile.Subnet, nicProfile.Gateway, nicProfile.DNS );
			}
			catch( Exception x )
			{
				UpdateStatus( "Error occured while setting network configuration." );
				UpdateStatus( x.Message );
				return;
			}

			UpdateStatus( "Done." );
		}

		/// <summary>
		/// Returns the profile for the specified NIC
		/// </summary>
		/// <param name="nicName"></param>
		/// <returns></returns>
		private NICProfile getNICProfile( string nicName )
		{
			foreach( NICProfile profile in _Profile.NICProfiles )
				if( profile.Name.Equals( nicName ) )
					return profile;

			return null;
		}

		/// <summary>
		/// Set Internet Explorer Proxt setting
		/// </summary>
		/// <param name="ieProfile"></param>
		private void applyIEProfile( IEProfile ieProfile )
		{
			UpdateStatus( "Setting Internet Explorer Proxy Setting..." );

			IEProxy.ProxyEnabled = ieProfile.UseProxy;
			IEProxy.ProxyServer = ieProfile.ProxyName;
			IEProxy.BypassProxyForLocal = ieProfile.BypassLocal;

			UpdateStatus( "Done." );
		}

		/// <summary>
		/// Raises event to update status
		/// </summary>
		/// <param name="message"></param>
		private void UpdateStatus( string message )
		{
			if( null != OnStatusUpdate )
				OnStatusUpdate( message + Environment.NewLine );
		}
		
		#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.140827.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