码迷,mamicode.com
首页 > Windows程序 > 详细

c#设置IP地址

时间:2015-06-18 19:21:40      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:

static void SetNetworkAdapter()
{
    ManagementBaseObject inPar = null;
    ManagementBaseObject outPar = null;
    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection moc = mc.GetInstances();
    foreach (ManagementObject mo in moc)
    {
        if (!(bool)mo["IPEnabled"])
            continue;

        //设置ip地址和子网掩码 
        inPar = mo.GetMethodParameters("EnableStatic");
        inPar["IPAddress"] = new string[] { "192.168.16.248", "192.168.16.249" };// 1.备用 2.IP
        inPar["SubnetMask"] = new string[] { "255.255.255.0", "255.255.255.0" };
        outPar = mo.InvokeMethod("EnableStatic", inPar, null);

        //设置网关地址 
        inPar = mo.GetMethodParameters("SetGateways");
        inPar["DefaultIPGateway"] = new string[] { "192.168.16.2", "192.168.16.254" }; // 1.网关;2.备用网关
        outPar = mo.InvokeMethod("SetGateways", inPar, null);

        //设置DNS 
        inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
        inPar["DNSServerSearchOrder"] = new string[] { "211.97.168.129", "202.102.152.3" }; // 1.DNS 2.备用DNS
        outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
        break;
    }
}

 

c#设置IP地址

标签:

原文地址:http://www.cnblogs.com/xinzheng/p/4586483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!