Click here to Skip to main content
15,881,757 members
Articles / Desktop Programming / Windows Forms

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.5K   1.8K   53  
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.
using System;
using System.Collections.Generic;
using System.Text;
using BusinessObjects;
using System.Management;
using System.Data;

namespace GUI
{
    public class WMIManager
    {
        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;
        }
        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;
                    }
                }
            }
        }
        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;
                }
            }
        }
    }
}

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
Team Leader
Turkey Turkey
Tamer Oz is a Microsoft MVP and works as Assistant Unit Manager.

Comments and Discussions