5,427,303 members and growing! (15,730 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Chameleon - Connection Settings Manager

By Tamer Oz

Chameleon is an application that allows users to easily change network configuration on each network adapter from taskbar. Chameleon offers the benefit of changing network settings according to the users location such as home or office.
C# 2.0, C#Windows, .NET, .NET 2.0, Win2K, WinXP, Win2003, VistaVS2005, Visual Studio, Architect, Dev

Posted: 1 Aug 2007
Updated: 1 Aug 2007
Views: 10,641
Bookmarked: 33 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
10 votes for this Article.
Popularity: 4.56 Rating: 4.56 out of 5
1 vote, 10.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 10.0%
4
8 votes, 80.0%
5

Screenshot - Chameleon.jpg

Introduction

Chameleon is an application that allows users to easily change network configuration on each network adapter from the taskbar. Chameleon offers the benefit of changing network settings according to the user's location such as home or office.

Background

The main idea of this application is changing network settings via WMI, I access WMI by using System.Management namespace. I have two objects named Connection and Profile. Connection is an object which is responsible for storing network connection information. Profile is an object which is responsible for storing profile(home, office etc.) settings such as IP, DNS, gateway etc.

The solution contains four projects. These projects are GUI, BusinessObjects, ControlLib and Helper. And there is also a setup project.

The sample application is running on the taskbar and users can change connection settings after right clicking by selecting connection and profile.

Using the code

There is a method which returns the list of available connections. This list is filled from the VMI provider.

public List<Connection> GetConnections()
{
        List<Connection> insListConnection = new List<Connection>();
        ManagementObjectSearcher m = new ManagementObjectSearcher();
        m.Query = new ObjectQuery
            ("Select * from Win32_NetworkAdapterConfiguration 
                    Where IPEnabled = True");
        foreach (ManagementObject mo in m.Get())
        {
            Connection c = new Connection();
            c.ConnectionId = mo["Index"].ToString();
            c.ConnectionName = mo["Caption"].ToString().Substring
                        (mo["Caption"].ToString().IndexOf("]") + 1).Trim();
            insListConnection.Add(c);
        }
        return insListConnection;
}

After creating an instance and filling the connection and profile object by passing these objects as parameter to the method named SetConnectionProfile, you can change the settings on the selected network adapter.

public void SetConnectionProfile(Connection c, Profile p)
{
    ManagementObjectSearcher m = new ManagementObjectSearcher();
    m.Query = new ObjectQuery("Select *
        from Win32_NetworkAdapterConfiguration Where IPEnabled = True");
    foreach (ManagementObject mo in m.Get())
    {
        if (mo["Index"].ToString()==c.ConnectionId)
        {
            mo.InvokeMethod("EnableStatic", new object[] {new string[]
                        { p.IpAddress }, new string[] { p.SubnetMask } });
                mo.InvokeMethod("SetGateways", new object[] {new string[]
                        { p.DefaultGateway }, new string[] { "1" } });
            mo.InvokeMethod("SetDNSServerSearchOrder", new object[]
                        {new string[] { p.PreferredDns} });
                    if (p.ProxyEnabled)
            {
                RegistryManager.EnableProxy = true;
                RegistryManager.BypassProxyForLocalAddresses = 
                        p.ByPassProxyForLocal;
                RegistryManager.ProxyAddress= p.ProxyAddress + ":" + 
                        p.ProxyPort;
            }
            else
            {
                RegistryManager.EnableProxy = false;
                RegistryManager.ProxyAddress = "";
                RegistryManager.BypassProxyForLocalAddresses =true;
            }
        }
    }
}

And there is another method named SetConnectionAutomatic that resets the connection settings.

public void SetConnectionAutomatic(Connection c)
{
    ManagementObjectSearcher m =new ManagementObjectSearcher();
    m.Query = new ObjectQuery("Select * from Win32_NetworkAdapterConfiguration
                                                    Where IPEnabled = True");
    foreach (ManagementObject mo in m.Get())
    {
        if (mo["Index"].ToString() == c.ConnectionId)
        {
            mo.InvokeMethod("EnableDHCP",null);
            mo.InvokeMethod("SetDNSServerSearchOrder",null);
            RegistryManager.EnableProxy = false;
            RegistryManager.ProxyAddress = "";
            RegistryManager.BypassProxyForLocalAddresses  = true;
        }
    }
}

Contact

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Tamer Oz


Tamer Oz is a Microsoft MVP and works as Assistant Unit Manager.
Occupation: Team Leader
Company: BilgeAdam
Location: Turkey Turkey

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralPLEASE add an option for setting a custom MAC adressmemberMilepp11:44 29 Jul '08  
GeneralHelal olsun.memberXess6:18 26 Jun '08  
GeneralAbiiii harikasin!!!memberfatihbural6:15 29 Nov '07  
GeneralMore than 1 gateway, dns,ipmemberdsand23:28 2 Oct '07  
GeneralRe: More than 1 gateway, dns,ipmembervarungupta2:51 18 Feb '08  
GeneralExcellent appmemberGutless9:41 18 Aug '07  
GeneralFINALLY!!!!memberMarko Vnucec IncitoIT23:38 8 Aug '07  
GeneralGreat appmembergiammin5:06 1 Aug '07  
GeneralRe: Great appmemberTamerOz5:12 1 Aug '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Aug 2007
Editor: Deeksha Shenoy
Copyright 2007 by Tamer Oz
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project