首页 ┆网站地图 ┆游戏资讯 ┆天龙八部 ┆资源下载 ┆淘宝网购物 ┆淘宝数码产品 ┆淘宝数码相机 
   设为首页
 加入收藏
 联系我们
 
 编程学堂> JAVA专区 | .NET专区 | PHP 专区 | ASP 专区 |  C/C++   | JAVASCRIPT |  数据库  | 软件工程 | 网络技术 | 网站制作 | 编程相关 | 硬件资讯
 业界相关> 业界新闻 | 操作系统 | 办公技巧 | 病毒安全 | 图形图像 |  常用工具  | 资源下载 | 网站推广 | 即时通讯 |  IT故事  |  IT健康  | QQ--开心网
 
 
 
 
 输入您的搜索字词  提交搜索表单          
   
 ※高级搜索

 您当前的位置: 主页 > NET专区 > C#语言 > C#实现更改IP功能源码
C#实现更改IP功能源码
来源: 发布时间:2008-02-01 浏览:309人次  字体:[大 中 小]  
 C#实现更改IP功能源码
using System;
using System.Management;
 
public class ChangeIP
{
    private ManagementBaseObject iObj = null;
    private ManagementBaseObject oObj = null;
    private ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
    private readonly ManagementObjectCollection moc;
 
    ///<summary>
    /// example:
    ///<code>
    /// ChangeIP o = new ChangeIP();
    /// string[] ipList = new string[]{"192.168.0.253","192.168.0.250"};
    /// string[] subnetList = new string[]{"255.255.255.0","255.255.255.0"};
    /// o.ChangeTo(ipList,subnetList);
    ///</code>
    ///</summary>
    public ChangeIP()
    {
        moc = mc.GetInstances();
    }
 
    ///<summary>cortrol</summary>
    ///<param name="ipAddr">IPAddr List</param>
    ///<param name="subnetMask">subnetMask List</param>
    public void ChangeTo(string[] ipAddr, string[] subnetMask)
    {
        foreach (ManagementObject mo in moc)
        {
            if (!(bool)mo["IPEnabled"]) continue;
 
            iObj = mo.GetMethodParameters("EnableStatic");
            iObj["IPAddress"] = ipAddr;
            iObj["SubnetMask"] = subnetMask;
            oObj = mo.InvokeMethod("EnableStatic", iObj, null);
        }
    }
 
    ///<summary>cortrol</summary>
    ///<param name="ipAddr">IPAddr List</param>
    ///<param name="subnetMask">subnetMask List</param>
    ///<param name="gateways">gateway List</param>
    ///<param name="gatewayCostMetric">gateway CostMetric List, example: 1</param>
    public void ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric)
    {
        foreach (ManagementObject mo in moc)
        {
            if (!(bool)mo["IPEnabled"]) continue;
 
            iObj = mo.GetMethodParameters("EnableStatic");
            iObj["IPAddress"] = ipAddr;
            iObj["SubnetMask"] = subnetMask;
            oObj = mo.InvokeMethod("EnableStatic", iObj, null);
 
            iObj = mo.GetMethodParameters("SetGateways");
            iObj["DefaultIPGateway"] = gateways;
            iObj["GatewayCostMetric"] = gatewayCostMetric;
            oObj = mo.InvokeMethod("SetGateways", iObj, null);
        }
    }
 
    ///<summary>cortrol</summary>
    ///<param name="ipAddr">IPAddr List</param>
    ///<param name="subnetMask">subnetMask List</param>
    ///<param name="gateways">gateway List</param>
    ///<param name="gatewayCostMetric">gateway CostMetric List, example: 1</param>
    ///<param name="dnsServer">DNSServer List</param>
    public void ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric, string[] dnsServer)
    {
        foreach (ManagementObject mo in moc)
        {
            if (!(bool)mo["IPEnabled"]) continue;
 
            iObj = mo.GetMethodParameters("EnableStatic");
            iObj["IPAddress"] = ipAddr;
            iObj["SubnetMask"] = subnetMask;
            oObj = mo.InvokeMethod("EnableStatic", iObj, null);
 
            iObj = mo.GetMethodParameters("SetGateways");
            iObj["DefaultIPGateway"] = gateways;
            iObj["GatewayCostMetric"] = gatewayCostMetric;
            oObj = mo.InvokeMethod("SetGateways", iObj, null);
 
            iObj = mo.GetMethodParameters("SetDNSServerSearchOrder");
            iObj["DNSServerSearchOrder"] = dnsServer;
            oObj = mo.InvokeMethod("SetDNSServerSearchOrder", iObj, null);
        }
    }
 
    ///<summary>DHCPEnabled</summary>
    public void EnableDHCP()
    {
        foreach (ManagementObject mo in moc)
        {
            if (!(bool)mo["IPEnabled"]) continue;
 
            if (!(bool)mo["DHCPEnabled"])
            {
                iObj = mo.GetMethodParameters("EnableDHCP");
                oObj = mo.InvokeMethod("EnableDHCP", iObj, null);
            }
        }
    }
}
 

 


本文转载:CSDN博客