Click here to Skip to main content
15,886,069 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 558.1K   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;

namespace SwitchNetConfig
{
	/// <summary>
	/// Stores network card configuration for a profile
	/// </summary>
	public class NICProfile
	{
		#region variables

		public string Name = string.Empty;

		public string IP = string.Empty;
		public string Subnet = string.Empty;
		public string Gateway = string.Empty;
		public string DNS = string.Empty;
		public bool UseDHCP = false;

		#endregion

		#region Constructors

		public NICProfile() {}

		public NICProfile( string name )
		{
			Name = name;
		}

		#endregion

		#region public methods

		public void IsValid()
		{
			// check IPs
			string [] IPs = IP.Split(',');
			if( 0 == IPs.Length )
				throw new Exception( "No IP specified" );

			// validate IP
			foreach( string ip in IPs )
				isValidIP( ip );

			// validate Subnet
			isValidIP( Subnet );

			// validate Gateway
			isValidIP( Gateway );

			// validate DNS
			string [] Dnses = DNS.Split(',');
			if( Dnses.Length > 0 )
			{
				foreach( string dns in Dnses )
					isValidIP( dns );
			}
		}

		private bool isValidIP( string IP )
		{
			string [] segments = IP.Split('.');

			if( segments.Length != 4 )
				throw new Exception( "Invalid IP format. Valid format is XXX.XXX.XXX.XXX" );

			foreach( string segment in segments )
			{
				try
				{
					byte value = byte.Parse( segment );
					return true;
				}
				catch
				{
					throw new Exception( "Only values from 0 to 255 allowed" );
				}
			}

			return false;
		}

		#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