Click here to Skip to main content
15,867,968 members
Articles / Desktop Programming / Windows Forms
Article

Chameleon - Connection Settings Manager

Rate me:
Please Sign up or sign in to vote.
4.84/5 (10 votes)
1 Aug 2007CPOL1 min read 52.4K   1.8K   53   9
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.

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.

C#
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.

C#
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.

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Turkey Turkey
Tamer Oz is a Microsoft MVP and works as Assistant Unit Manager.

Comments and Discussions

 
GeneralWireless adapter not recognized.... Pin
Bit Builder27-Jul-12 11:14
Bit Builder27-Jul-12 11:14 
GeneralPLEASE add an option for setting a custom MAC adress Pin
Milepp29-Jul-08 10:44
Milepp29-Jul-08 10:44 
GeneralHelal olsun. Pin
Xess26-Jun-08 5:18
Xess26-Jun-08 5:18 
GeneralMore than 1 gateway, dns,ip Pin
dsand2-Oct-07 22:28
dsand2-Oct-07 22:28 
GeneralRe: More than 1 gateway, dns,ip Pin
varungupta18-Feb-08 1:51
varungupta18-Feb-08 1:51 
GeneralExcellent app Pin
Gutless18-Aug-07 8:41
Gutless18-Aug-07 8:41 
GeneralFINALLY!!!! Pin
Marko Vnucec IncitoIT8-Aug-07 22:38
Marko Vnucec IncitoIT8-Aug-07 22:38 
GeneralGreat app Pin
giammin1-Aug-07 4:06
giammin1-Aug-07 4:06 
GeneralRe: Great app Pin
Tamer Oz1-Aug-07 4:12
Tamer Oz1-Aug-07 4:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.