|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis is a tiny utility which stores multiple network and proxy configuration as profiles. You can apply a profile to set specific network setting and proxy from simple shortcuts. BackgroundIf you have a laptop, when you go to different places, you have to change Network configuration and browser proxy. I have to go to 4 different places everyday with my laptop; my university, my offices and my home. Everywhere I have to change my IP setting and IE Proxy. I could write a WMI Script to do all of them. Unfortunately, for some reason WMI is not working from VB Script in my laptop but somehow it is working from .NET. So, I have made a handy utility which I can use to store configuration profiles for different places and apply them very quickly just by pressing one key and a ENTER at system startup. This program automatically sets IP, Subnet, Gateway and DNS for my LAN card and also changes the IE Proxy setting. Using the codeYou can setup in 3 easy steps.
It’s even easier to use:
Points of InterestIt’s a very simple Winforms application written in C#. The key technologies are:
WMIWindows Management Intrumentation (WMI) helps you work with Network configuration. For example, the following code retrieves Network Setting for a given Network Card.
/// <summary>
/// Returns the network card configuration of the specified NIC
/// </summary>
/// <PARAM name="nicName">Name of the NIC</PARAM>
/// <PARAM name="ipAdresses">Array of IP</PARAM>
/// <PARAM name="subnets">Array of subnet masks</PARAM>
/// <PARAM name="gateways">Array of gateways</PARAM>
/// <PARAM name="dnses">Array of DNS IP</PARAM>
public static void GetIP( string nicName, out string [] ipAdresses,
out string [] subnets, out string [] gateways, out string [] dnses )
{
ipAdresses = null;
subnets = null;
gateways = null;
dnses = null;
ManagementClass mc = new ManagementClass(
"Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
// Make sure this is a IP enabled device.
// Not something like memory card or VM Ware
if( mo["ipEnabled"] as bool )
{
if( mo["Caption"].Equals( nicName ) )
{
ipAdresses = (string[]) mo["IPAddress"];
subnets = (string[]) mo["IPSubnet"];
gateways = (string[]) mo["DefaultIPGateway"];
dnses = (string[]) mo["DNSServerSearchOrder"];
break;
}
}
}
}
“Win32_NetworkAdapterConfiguration” gives you the collection of network adapters installed. Be careful, all the entries in this list may not be your LAN card. Similarly you can set Network configuration just by a couple of lines.
/// <summary>
/// Set IP for the specified network card name
/// </summary>
/// <PARAM name="nicName">Caption of the network card</PARAM>
/// <PARAM name="IpAddresses">Comma delimited string
/// containing one or more IP</PARAM>
/// <PARAM name="SubnetMask">Subnet mask</PARAM>
/// <PARAM name="Gateway">Gateway IP</PARAM>
/// <PARAM name="DnsSearchOrder">Comma delimited DNS IP</PARAM>
public static void SetIP( string nicName, string IpAddresses,
string SubnetMask, string Gateway, string DnsSearchOrder)
{
ManagementClass mc = new ManagementClass(
"Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
// Make sure this is a IP enabled device.
// Not something like memory card or VM Ware
if( mo["IPEnabled"] as bool )
{
if( mo["Caption"].Equals( nicName ) )
{
ManagementBaseObject newIP =
mo.GetMethodParameters( "EnableStatic" );
ManagementBaseObject newGate =
mo.GetMethodParameters( "SetGateways" );
ManagementBaseObject newDNS =
mo.GetMethodParameters( "SetDNSServerSearchOrder" );
newGate[ "DefaultIPGateway" ] = new string[] { Gateway };
newGate[ "GatewayCostMetric" ] = new int[] { 1 };
newIP[ "IPAddress" ] = IpAddresses.Split( ',' );
newIP[ "SubnetMask" ] = new string[] { SubnetMask };
newDNS[ "DNSServerSearchOrder" ] = DnsSearchOrder.Split(',');
ManagementBaseObject setIP = mo.InvokeMethod(
"EnableStatic", newIP, null);
ManagementBaseObject setGateways = mo.InvokeMethod(
"SetGateways", newGate, null);
ManagementBaseObject setDNS = mo.InvokeMethod(
"SetDNSServerSearchOrder", newDNS, null);
break;
}
}
}
}
Internet Explorer ProxyChanging Internet Explorer Proxy setting is a tricky job. There are 3 registry keys that you need to handle: CURRENT_USER\ Software\Microsoft\Windows\CurrentVersion\Internet Settings
XML SerializationXML Serialization makes it very easy to persist object graph in human readable form. For example, this is the XML that the program generates: <?xml version="1.0"?>
<ConfigWrapper xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Profiles>
<anyType xsi:type="Profile">
<Name>Home</Name>
<NICProfiles>
<anyType xsi:type="NICProfile">
<Name>[00000001] Intel(R) PRO/100
VE Network Connection</Name>
<IP>192.168.2.43</IP>
<Subnet>255.255.255.0</Subnet>
<Gateway>192.168.2.1</Gateway>
<DNS>202.141.190.2,202.141.190.3</DNS>
</anyType>
</NICProfiles>
<IEProfile>
<UseProxy>true</UseProxy>
<ProxyName>192.168.2.1:3128</ProxyName>
<BypassLocal>true</BypassLocal>
<BypassAddresses />
</IEProfile>
</anyType>
It makes it very easy to modify the file manually. You can save an entire object graph just by writing 3 lines of code:
// 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 );
// Serialize the array list to the file
serializer.Serialize( writer.BaseStream, wrapper );
History
| ||||||||||||||||||||