Click here to Skip to main content
12,703,067 members (29,174 online)
Click here to Skip to main content
Articles » Languages » C# » Applications » Downloads

Stats

444K views
17.1K downloads
197 bookmarked
Posted

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

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

Share

About the Author


You may also be interested in...

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