65.9K
CodeProject 正在变化。 阅读更多。
Home

Chameleon - 连接设置管理器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.84/5 (9投票s)

2007 年 8 月 1 日

CPOL

1分钟阅读

viewsIcon

54688

downloadIcon

1872

Chameleon 是一款应用程序,允许用户从任务栏轻松更改每个网络适配器的网络配置。 Chameleon 具有根据用户位置(例如家庭或办公室)更改网络设置的优势。

Screenshot - Chameleon.jpg

引言

Chameleon 是一款应用程序,允许用户从任务栏轻松更改每个网络适配器的网络配置。 Chameleon 具有根据用户的位置(例如家庭或办公室)更改网络设置的优势。

背景

此应用程序的主要思想是通过 WMI 更改网络设置,我使用 System.Management 命名空间访问 WMI。 我有两个对象,名为 Connection ProfileConnection 是一个负责存储网络连接信息的对象。 Profile 是一个负责存储配置文件(家庭、办公室等)设置(例如 IP、DNS、网关等)的对象。

该解决方案包含四个项目。 这些项目是 GUIBusinessObjectsControlLib Helper。 还有一个安装项目。

示例应用程序在任务栏上运行,用户可以在右键单击后通过选择连接和配置文件来更改连接设置。

使用代码

有一个方法会返回可用连接的列表。 该列表是从 VMI 提供程序填充的。

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;
}

创建实例并填充连接和配置文件对象后,通过将这些对象作为参数传递给名为 SetConnectionProfile 的方法,您可以更改所选网络适配器上的设置。

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;
            }
        }
    }
}

还有另一个名为 SetConnectionAutomatic 的方法,可以重置连接设置。

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;
        }
    }
}

联系方式

© . All rights reserved.